Skip to content
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鈥檒l 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

Open
3 tasks done
wnnce opened this issue May 20, 2024 · 3 comments
Open
3 tasks done

Comments

@wnnce
Copy link

wnnce commented May 20, 2024

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.
image

Code Snippet (optional)

var fiberClient *client.Client

func init() {
	fiberClient = client.New()
}

func uploadFile(fileName string, reader io.ReadCloser) {
	uploadToken := makeUploadToken(fileName, 1*time.Hour)
	formFile := client.AcquireFile(func(f *client.File) {
		f.SetName("file")
		f.SetFieldName(fileName)
		f.SetReader(reader)
	})
	request := client.AcquireRequest()
	request.AddFormData("key", fileName)
	request.AddFormData("token", uploadToken)
	request.AddFiles(formFile)
	response, err := request.Post(REGION)
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println(string(response.Body()))
}

Checklist:

  • I agree to follow Fiber's Code of Conduct.
  • I have checked for existing issues that describe my questions prior to opening this one.
  • I understand that improperly formatted questions may be closed without explanation.
Copy link

welcome bot commented May 20, 2024

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

@ReneWerner87
Copy link
Member

@efectn can you help here

@wnnce
Copy link
Author

wnnce commented May 21, 2024

@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()))
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants