Add a Config for multipart.NewWriter #36
Answered
by
Crocmagnon
earthboundkid
asked this question in
Ideas
-
API could look like |
Beta Was this translation helpful? Give feedback.
Answered by
Crocmagnon
Aug 26, 2024
Replies: 2 comments 2 replies
-
do we have example for body multypart like this postman ? @carlmjohnson |
Beta Was this translation helpful? Give feedback.
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) |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
earthboundkid
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I just had the need to send multipart data to a picky server. Here's what I came up with, if others are interested: