Add a Config for multipart.NewWriter #36
|
API could look like |
Answered by
earthboundkid
Nov 21, 2024
Replies: 3 comments 2 replies
|
do we have example for body multypart like this postman ? @carlmjohnson |
1 reply
|
I just had the need to send multipart data to a picky server. Here's what I came up with, if others are interested: boundary := randomBoundary()
err := requests.URL(url).
Method(http.MethodPost).
Header("Content-Type", "multipart/form-data; boundary="+boundary).
BodyWriter(func(w io.Writer) error {
multi := multipart.NewWriter(w)
if err := multi.SetBoundary(boundary); err != nil {
return fmt.Errorf("setting boundary: %w", err)
}
writer, err := multi.CreateFormFile("file", filename)
if err != nil {
return fmt.Errorf("creating form file: %w", err)
}
if _, err := io.Copy(writer, reader); err != nil {
return fmt.Errorf("copying file: %w", err)
}
if err := multi.Close(); err != nil {
return fmt.Errorf("closing multipart writer: %w", err)
}
return nil
}).
Fetch(ctx) |
1 reply
|
https://github.com/earthboundkid/requests/pull/119/files was merged. |
0 replies
Answer selected by
earthboundkid
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment

https://github.com/earthboundkid/requests/pull/119/files was merged.