valyala / fasthttp

Fast HTTP package for Go. Tuned for high performance. Zero memory allocations in hot paths. Up to 10x faster than net/http
MIT License
21.94k stars 1.76k forks source link

Creating multipart for client request #1574

Closed whyynoot closed 1 year ago

whyynoot commented 1 year ago

Hello, I have an http client and trying to figure out how do I set multipart form data

multipart, _ := request.MultipartForm()
multipart.Value["data"][0] = "642af4bc2d41a0000157d467"

i tried smth like this but this doesnt works for me, can u please help me with that

erikdubbelboer commented 1 year ago

request.MultipartForm() is only for incoming requests, not for outgoing.

You will have to use multipart.NewWriter() and add the fields to that. Then you can either use io.ReadAll to read the body into a byte slice and use request.SetBody() to set it as body or use request.SetBodyStream(). Don't forget to call request.Header.SetMultipartFormBoundary() with the return value of multipart.Writer.Boundary().