formdata

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2023 License: Apache-2.0 Imports: 6 Imported by: 0

README

go-form-data

multipart/form-data without memory buffering.

Example

package main

import (
	formdata "github.com/jc-lab/go-form-data"
	"log"
	"net/http"
	"os"
)

func main() {
	formData := formdata.NewFormData()

	formData.AddDataField("aaaa", "text/plain", []byte("hello world"))

	a, _ := os.OpenFile("aaa.txt", os.O_RDONLY, 0)
	formData.AddFileField("files[]", "application/octet-stream", a)
	
	b, _ := os.OpenFile("bbb.txt", os.O_RDONLY, 0)
	formData.AddFileField("files[]", "application/octet-stream", b)

	req, err := http.NewRequest("POST", "http://127.0.0.1/api/upload", formData)
	if err != nil {
		log.Fatalln(err)
	}
	
	resp, err := formData.Do(http.DefaultClient, req)
	if err != nil {
		log.Fatalln(err)
	}

	println(resp.Status)
	println(resp.StatusCode)
	println(resp.Body)
}

License

Apache-2.0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FormData

type FormData struct {
	io.ReadCloser
	// contains filtered or unexported fields
}

func NewFormData

func NewFormData() *FormData

func (*FormData) AddDataField

func (f *FormData) AddDataField(name string, contentType string, data []byte)

func (*FormData) AddFileField

func (f *FormData) AddFileField(name string, contentType string, file *os.File) error

func (*FormData) Boundary

func (f *FormData) Boundary() string

func (*FormData) Close

func (f *FormData) Close() error

func (*FormData) Do

func (f *FormData) Do(client HttpClientable, req *http.Request) (*http.Response, error)

func (*FormData) Read

func (f *FormData) Read(p []byte) (int, error)

type HttpClientable

type HttpClientable interface {
	Do(req *http.Request) (*http.Response, error)
}

Jump to

Keyboard shortcuts

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