An open API service indexing awesome lists of open source software.

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.

Awesome Lists containing this project

README

          

# go-multipart-helpers

[![PkgGoDev](https://pkg.go.dev/badge/github.com/prantlf/go-multipart-helpers)](https://pkg.go.dev/github.com/prantlf/go-multipart-helpers)
[![Test Status](https://github.com/prantlf/go-multipart-helpers/workflows/Test/badge.svg)](https://github.com/prantlf/go-multipart-helpers/actions)
[![codecov](https://codecov.io/gh/prantlf/go-multipart-helpers/branch/master/graph/badge.svg?token=XS0COPSRR7)](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