Skip to content

Add a Config for multipart.NewWriter #36

Answered by Crocmagnon
earthboundkid asked this question in Ideas
Discussion options

You must be logged in to vote

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

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@earthboundkid
Comment options

Comment options

You must be logged in to vote
1 reply
@earthboundkid
Comment options

Answer selected by earthboundkid
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Ideas
Labels
None yet
3 participants