-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
🤗 [Question]: Does fiber v3's Request not have the function of adding files to FormData? #3007
Comments
Thanks for opening your first issue here! 🎉 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord |
@efectn can you help here |
I implemented the requirement using fasthttp's multipart.NewWriter() and RawRequest func upload(fileName string, reader io.ReadCloser) {
uploadToken := makeUploadToken(fileName, 1*time.Hour)
request := client.AcquireRequest()
requestBody := &bytes.Buffer{}
multipartWriter := multipart.NewWriter(requestBody)
fileWriter, err := multipartWriter.CreateFormFile("file", fileName)
if err != nil {
log.Fatalln(err)
}
if _, err = io.Copy(fileWriter, reader); err != nil {
log.Fatalln(err)
}
if err = multipartWriter.WriteField("key", fileName); err != nil {
log.Fatalln(err)
}
if err = multipartWriter.WriteField("token", uploadToken); err != nil {
log.Fatalln(err)
}
if err = multipartWriter.Close(); err != nil {
log.Fatalln(err)
}
request.SetRawBody(requestBody.Bytes())
request.AddHeader("Content-Type", multipartWriter.FormDataContentType())
response, err := request.Post(REGION)
if err != nil {
log.Fatalln(err)
}
fmt.Println(string(response.Body()))
} |
same request here |
What you want is already done at hooks stage https://github.com/gofiber/fiber/blob/main/client/hooks.go#L271 while sending the request. Which cases does this hook not cover? Do you want to get request body before sending the request? @carlos19960601 @wnnce |
@wnnce io.Copy would load the whole thing to memory. Not ideal if the file is big. |
Question Description
I have an interface that needs to use FormData to upload files, but the AddFormDate() provided can only add strings, while AddFile() adds the file to the files field and not to the FormDate.
Code Snippet (optional)
Checklist:
The text was updated successfully, but these errors were encountered: