helpers

package module
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 14, 2020 License: MIT Imports: 8 Imported by: 0

README

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:

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

Overview

Package helpers helps writing file parts to MIME multipart messages while preserving the original content type inferred from the file extension.

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, thi spackage will help you.

Files can be written using their path by a convenience method:

message := &bytes.Buffer{}
writer := multipart.NewWriter(message)
err := helpers.WriteFile(writer, "file", "test.txt")
err := writer.Close()
Example
package main

import (
	"bytes"
	"log"
	"mime/multipart"

	helpers "github.com/prantlf/go-multipart-helpers"
	"github.com/prantlf/go-multipart-helpers/demo"
)

func main() {
	// Create a new buffer for the message content.
	message := &bytes.Buffer{}
	// Create a new multipart message writrer with a random boundary.
	writer := multipart.NewWriter(message)
	// Write a textual field.
	if err := writer.WriteField("comment", "a comment"); err != nil {
		log.Fatal(err)
	}
	// Write a file.
	if err := helpers.WriteFile(writer, "file", "demo/test.txt"); err != nil {
		log.Fatal(err)
	}
	// Finalize rhe message by appending the trailing boundary separatore.
	if err := writer.Close(); err != nil {
		log.Fatal(err)
	}

	// Make a network request with the composed content type and request body.
	demo.PrintRequest(writer.FormDataContentType(), message)
}
Output:

Content-Type: multipart/form-data; boundary=1879bcd06ac39a4d8fa5
Content-Length: 383

--1879bcd06ac39a4d8fa5
Content-Disposition: form-data; name="comment"

a comment
--1879bcd06ac39a4d8fa5
Content-Disposition: form-data; name="file"; filename="test.txt"
Content-Type: text/plain; charset=utf-8

text file content
--1879bcd06ac39a4d8fa5--

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateFilePart

func CreateFilePart(writer *multipart.Writer, fieldName, fileName string) (io.Writer, error)

CreateFilePart creates a new form-data header with the provided field name, file name and its content type inferred from its file extension. It calls CreatePart to create a new multipart section with the provided header. The content of the file should be written to the returned Writer.

Example
package main

import (
	"bytes"
	"log"
	"mime/multipart"

	helpers "github.com/prantlf/go-multipart-helpers"
	"github.com/prantlf/go-multipart-helpers/demo"
)

func main() {
	message := &bytes.Buffer{}
	writer := multipart.NewWriter(message)

	// Create an empty file part.
	if _, err := helpers.CreateFilePart(writer, "file", "test.txt"); err != nil {
		log.Fatal(err)
	}

	writer.Close()
	demo.PrintRequest(writer.FormDataContentType(), message)
}
Output:

Content-Type: multipart/form-data; boundary=1879bcd06ac39a4d8fa5
Content-Length: 241

--1879bcd06ac39a4d8fa5
Content-Disposition: form-data; name="file"; filename="test.txt"
Content-Type: text/plain; charset=utf-8

--1879bcd06ac39a4d8fa5--

func WriteFile

func WriteFile(writer *multipart.Writer, fieldName, filePath string) error

WriteFile calls CreateFilePart and then writes the file content to the part writer.

Example
package main

import (
	"bytes"
	"log"
	"mime/multipart"

	helpers "github.com/prantlf/go-multipart-helpers"
	"github.com/prantlf/go-multipart-helpers/demo"
)

func main() {
	message := &bytes.Buffer{}
	writer := multipart.NewWriter(message)

	// Write a file.
	if err := helpers.WriteFile(writer, "file", "demo/test.bin"); err != nil {
		log.Fatal(err)
	}

	writer.Close()
	demo.PrintRequest(writer.FormDataContentType(), message)
}
Output:

Content-Type: multipart/form-data; boundary=1879bcd06ac39a4d8fa5
Content-Length: 259

--1879bcd06ac39a4d8fa5
Content-Disposition: form-data; name="file"; filename="test.bin"
Content-Type: application/octet-stream

binary file content
--1879bcd06ac39a4d8fa5--

func WriteFileReader

func WriteFileReader(writer *multipart.Writer, fieldName, fileName string, reader io.Reader) error

WriteFileReader calls CreateFilePart and then writes the reader content to the part writer.

Example
package main

import (
	"bytes"
	"log"
	"mime/multipart"
	"strings"

	helpers "github.com/prantlf/go-multipart-helpers"
	"github.com/prantlf/go-multipart-helpers/demo"
)

func main() {
	message := &bytes.Buffer{}
	writer := multipart.NewWriter(message)

	// Write a file.
	if err := helpers.WriteFileReader(writer, "file", "test.txt",
		strings.NewReader("test")); err != nil {
		log.Fatal(err)
	}

	writer.Close()
	demo.PrintRequest(writer.FormDataContentType(), message)
}
Output:

Content-Type: multipart/form-data; boundary=1879bcd06ac39a4d8fa5
Content-Length: 245

--1879bcd06ac39a4d8fa5
Content-Disposition: form-data; name="file"; filename="test.txt"
Content-Type: text/plain; charset=utf-8

test
--1879bcd06ac39a4d8fa5--

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL