https://github.com/prantlf/go-multipart-helpers
Helps writing file parts to MIME multipart messages while preserving the original content type inferred from the file extension.
https://github.com/prantlf/go-multipart-helpers
form-data go go-module go-package helpers multipart multipart-formdata
Last synced: 7 months ago
JSON representation
Helps writing file parts to MIME multipart messages while preserving the original content type inferred from the file extension.
- Host: GitHub
- URL: https://github.com/prantlf/go-multipart-helpers
- Owner: prantlf
- License: mit
- Created: 2020-11-13T14:37:16.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2022-01-29T13:50:57.000Z (about 4 years ago)
- Last Synced: 2025-02-01T10:43:05.482Z (about 1 year ago)
- Topics: form-data, go, go-module, go-package, helpers, multipart, multipart-formdata
- Language: Go
- Homepage:
- Size: 10.7 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-multipart-helpers
[](https://pkg.go.dev/github.com/prantlf/go-multipart-helpers)
[](https://github.com/prantlf/go-multipart-helpers/actions)
[](https://codecov.io/gh/prantlf/go-multipart-helpers)
Helps writing file parts to MIME multipart messages according to [RFC7578] while preserving the original content type inferred from the file extension. See the [documentation] for more information.
`CreateFormFile` from [multipart writer] sets the content type always to `application/octet-stream`. If you need to preserve the content type of the file decided by its file extension, choose a function among `CreateFilePart`,` WriteFile` or `WriteFileReader` from this package.
## Installation
Add this package to `go.mod` and `go.sub` in your Go project:
go get github.com/prantlf/go-multipart-helpers
## Usage
Upload a file with comment:
```go
import (
"net/http"
helpers "github.com/prantlf/go-multipart-helpers"
)
// compose a multipart form-data content
message := &bytes.Buffer{}
writer := multipart.NewWriter(message)
comp.AddField("comment", "a comment")
err := helpers.WriteFile(writer, "file", "test.txt")
// post a request with the generated content type and body
resp, err := http.DefaultClient.Post("http://host.com/upload",
writer.FormDataContentType(), message)
```
See the [documentation] for the full interface.
[documentation]: https://pkg.go.dev/github.com/prantlf/go-multipart-helpers
[RFC7578]: https://tools.ietf.org/html/rfc7578
[multipart writer]: https://golang.org/pkg/mime/multipart/#Writer