formcontent

package module
v0.0.0-...-157ce10 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2018 License: Apache-2.0 Imports: 6 Imported by: 0

README

This helper library allows you upload large file without loading the file content into the memory.

Build Status

example:

package main

import (
	"github.com/fredwangwang/formcontent"
	"net/http"
	"log"
	"io/ioutil"
	"fmt"
)

func main() {
	var err error

	client := &http.Client{}

	uri := "https://full.qualified.domain.name/post/route"

	// initialize multipart form
	multipart := formcontent.NewForm()

	// add fields to the form
	if err = multipart.AddFile("file", "/path/to/the/file.ext"); err != nil {
		log.Fatal(err)
	}
	if err = multipart.AddField("attribute1", "value1"); err != nil {
		log.Fatal(err)
	}
	if err = multipart.AddField("attribute2", "value2"); err != nil {
		log.Fatal(err)
	}

	// finish editing the form
	form := multipart.Finalize()

	// create a request
	req, err := http.NewRequest("POST", uri, form.Content)
	if err != nil {
		log.Fatal(err)
	}
	req.Header.Set("Content-Type", form.ContentType)
	req.ContentLength = form.ContentLength

	resp, err := client.Do(req)
	if err != nil {
		log.Fatal(err)
	}

	defer resp.Body.Close()
	respContent, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(resp.StatusCode)
	fmt.Println(resp.Header)
	fmt.Println(string(respContent))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ContentSubmission

type ContentSubmission struct {
	Content       io.Reader
	ContentType   string
	ContentLength int64
}

type Form

type Form struct {
	// contains filtered or unexported fields
}

func NewForm

func NewForm() *Form

func (*Form) AddField

func (f *Form) AddField(key string, value string) error

func (*Form) AddFile

func (f *Form) AddFile(key string, path string) error

func (*Form) Finalize

func (f *Form) Finalize() ContentSubmission

Jump to

Keyboard shortcuts

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