gowrex

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2017 License: MIT Imports: 8 Imported by: 1

README

Gowrex - Golang Multipart File Upload Library

Go Report Card GoDoc

Why Gowrex?

Easily create multipart form/file uploads and JSON requests.

Currently this is the only public Go library that supports multiform file uploads out of the box.

Features:

  • Simple, stable, and idomatic Go API
  • Multipart file & form uploading
  • JSON RESTful Requests
  • Supports httptest for local router testing
  • Add Custom Headers
  • Connection Timeout
  • Basic Auth

Install

  go get github.com/bevanhunt/gowrex

Documentation

Example of JSON POST with connection timeout


// JSONReceive - json response
type JSONReceive struct {
	ID     int64  `json:"id"`
	Title  string `json:"title"`
	Body   string `json:"body"`
	UserID int64  `json:"userId"`
}

// JSONSend - json post
type JSONSend struct {
	Title  string `json:"title"`
	Body   string `json:"body"`
	UserID int64  `json:"userId"`
}

func main() {
	timeout := 10 * time.Second
	jsonData := &JSONSend{
		Title:  "fancy book",
		Body:   "this is a fancy book",
		UserID: 12,
	}
	req, err := gowrex.Request{
		URI:     "http://jsonplaceholder.typicode.com/posts",
		Timeout: timeout}.PostJSON(jsonData)
	if err != nil {
		log.Println(err)
	}
	res, err := req.Do()
	if err != nil {
		log.Println(err)
	}
	resp := &JSONReceive{}
	res.JSON(resp)
	// should print - this is a fancy book
	fmt.Println(resp.Body)
}

Similar libraries

TODO

  • tests
  • cookies
  • more examples

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BasicAuth added in v0.0.5

type BasicAuth struct {
	Username string
	Password string
}

BasicAuth - a basic auth object

type Header struct {
	Key   string
	Value string
}

Header - a header object

type Request

type Request struct {
	URI       string
	Req       *http.Request
	Timeout   time.Duration
	Headers   []Header
	BasicAuth BasicAuth
}

Request - the request object

func (Request) AddBasicAuth added in v0.0.5

func (r Request) AddBasicAuth(username string, password string) Request

AddBasicAuth - add basic auth on the request

func (Request) AddHeader

func (r Request) AddHeader(key string, value string) Request

AddHeader - add a header on the request

func (Request) Do

func (r Request) Do() (Response, error)

Do - process the request with timeout

func (Request) Get

func (r Request) Get() (Request, error)

Get - GET request to any endpoint

func (Request) GetJSON

func (r Request) GetJSON() (Request, error)

GetJSON - GET request to a JSON endpoint

func (Request) PostForm

func (r Request) PostForm(params map[string]string) (Request, error)

PostForm - POST request for a multipart form data

func (Request) PostFormFile

func (r Request) PostFormFile(params map[string]string, paramName string, fileName string, fileBuffer *bytes.Buffer) (Request, error)

PostFormFile - POST request for a multipart upload with file buffer with optional params

func (Request) PostFormFileDisk

func (r Request) PostFormFileDisk(params map[string]string, paramName string, filePath string) (Request, error)

PostFormFileDisk - POST request for a multipart upload with file path with optional params

func (Request) PostJSON

func (r Request) PostJSON(body interface{}) (Request, error)

PostJSON - POST request to a JSON endpoint

func (Request) PutForm

func (r Request) PutForm(params map[string]string) (Request, error)

PutForm - PUT request for a multipart form data

func (Request) PutFormFile

func (r Request) PutFormFile(params map[string]string, paramName string, fileName string, fileBuffer *bytes.Buffer) (Request, error)

PutFormFile - PUT request for a multipart upload with file buffer with optional params

func (Request) PutFormFileDisk

func (r Request) PutFormFileDisk(params map[string]string, paramName string, filePath string) (Request, error)

PutFormFileDisk - PUT request for a multipart upload with file path with optional params

func (Request) PutJSON

func (r Request) PutJSON(body interface{}) (Request, error)

PutJSON - PUT request to a JSON endpoint

type Response

type Response struct {
	Res *http.Response
	URI string
}

Response - the response object

func (Response) JSON

func (e Response) JSON(decoder interface{}) (*interface{}, error)

JSON - decode JSON to interface

Jump to

Keyboard shortcuts

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