Add a Config for multipart.NewWriter #36
-
API could look like |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
do we have example for body multypart like this postman ? @carlmjohnson |
Beta Was this translation helpful? Give feedback.
-
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) |
Beta Was this translation helpful? Give feedback.
-
https://github.com/earthboundkid/requests/pull/119/files was merged. |
Beta Was this translation helpful? Give feedback.
https://github.com/earthboundkid/requests/pull/119/files was merged.