https://github.com/prantlf/go-multipart-composer
Prepares bodies of HTTP requests with MIME multipart messages without reading entire file contents to memory.
https://github.com/prantlf/go-multipart-composer
form-data go go-module go-package multipart multipart-formdata streaming
Last synced: about 2 months ago
JSON representation
Prepares bodies of HTTP requests with MIME multipart messages without reading entire file contents to memory.
- Host: GitHub
- URL: https://github.com/prantlf/go-multipart-composer
- Owner: prantlf
- License: mit
- Created: 2020-11-12T08:38:27.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2022-01-29T15:52:32.000Z (over 3 years ago)
- Last Synced: 2025-02-01T10:43:05.364Z (4 months ago)
- Topics: form-data, go, go-module, go-package, multipart, multipart-formdata, streaming
- Language: Go
- Homepage:
- Size: 23.4 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-multipart-composer
[](https://pkg.go.dev/github.com/prantlf/go-multipart-composer)
[](https://github.com/prantlf/go-multipart-composer/actions)
[](https://codecov.io/gh/prantlf/go-multipart-composer)Prepares bodies of HTTP requests with MIME multipart messages according to [RFC7578] without reading entire file contents to memory. Instead of writing files to a [multipart writer] right away, it collects [readers] for each part of the form and lets them stream to the network once the request has been sent. Avoids buffering of the request body simpler than with [goroutines] and [pipes]. See the [documentation] for more information.
## Installation
Add this package to `go.mod` and `go.sub` in your Go project:
go get github.com/prantlf/go-multipart-composer
## Usage
Upload a file with comment:
```go
import (
"net/http"
composer "github.com/prantlf/go-multipart-composer"
)
// compose a multipart form-data content
comp := composer.NewComposer()
comp.AddField("comment", "a comment")
err := comp.AddFile("file", "test.txt")
// post a request with the generated content type and body
resp, err := http.DefaultClient.Post("http://host.com/upload",
comp.FormDataContentType(), comp.DetachReader())
```If the server does not support chunked encoding and requires `Content-=Length` in the header:
```go
comp := composer.NewComposer()
comp.AddField("comment", "a comment")
err := comp.AddFile("file", "test.txt")
reqBody, contentLength, err := comp.DetachReaderWithSize()
if err != nil {
comp.Close() // DetachReaderWithSize does not close the composer on failure
log.Fatal(err)
}
// post a request with the generated body, content type and content length
req, err := http.NewRequest("POST", "http://host.com/upload", reqBody)
req.Header.Add("Content-Type", comp.FormDataContentType())
req.ContentLength = contentLength
resp, err := http.DefaultClient.Do(request)
```See the [documentation] for the full interface.
[documentation]: https://pkg.go.dev/github.com/prantlf/go-multipart-composer#section-documentation
[readers]: https://golang.org/pkg/io/#Reader
[multipart writer]: https://golang.org/pkg/mime/multipart/#Writer
[goroutines]: https://tour.golang.org/concurrency/1
[pipes]: https://golang.org/pkg/io/#Pipe
[RFC7578]: https://tools.ietf.org/html/rfc7578