simplehttp

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2016 License: BSD-3-Clause, Apache-2.0 Imports: 10 Imported by: 0

README

simplehttp

Build Status

Simple HTTP library for Go.

This small library adds some utility functions for doing HTTP request and easilly gettings results as structs from JSON and XML.

Supports alternative http.Client instances to support use on Google App Engine.

Examples are coming soon.

The code is released under a 3-clause BSD license. See the LICENSE file for more information.

Documentation

Overview

Package simplehttp provides some simple methods and types to do HTTP queries with form values and parameters easily - especially if the returned result is expected to be JSON or XML.

Author: Michael Banzon

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetJSONInput

func GetJSONInput(r *http.Request, w http.ResponseWriter, v interface{}) (err error)

func OutputJSON

func OutputJSON(w http.ResponseWriter, v interface{}) (err error)

Types

type BasicAuthentication

type BasicAuthentication struct {
	User     string
	Password string
}

Type to encapsulate basic authentication for requests.

type FormDataPayload

type FormDataPayload struct {
	Values      []keyValuePair
	Files       []keyValuePair
	ReadClosers []keyNameRC
	// contains filtered or unexported fields
}

func NewFormDataPayload

func NewFormDataPayload() *FormDataPayload

func (*FormDataPayload) AddFile

func (f *FormDataPayload) AddFile(key, file string)

func (*FormDataPayload) AddReadCloser

func (f *FormDataPayload) AddReadCloser(key, name string, rc io.ReadCloser)

func (*FormDataPayload) AddValue

func (f *FormDataPayload) AddValue(key, value string)

func (*FormDataPayload) GetContentType

func (f *FormDataPayload) GetContentType() string

func (*FormDataPayload) GetPayloadBuffer

func (f *FormDataPayload) GetPayloadBuffer() (*bytes.Buffer, error)

type HTTPRequest

type HTTPRequest struct {
	URL               string
	Parameters        map[string][]string
	Headers           map[string]string
	BasicAuthUser     string
	BasicAuthPassword string
	Client            *http.Client
}

Holds all information used to make a HTTP request.

func NewHTTPRequest

func NewHTTPRequest(url string) *HTTPRequest

Creates a new HTTPRequest instance.

func (*HTTPRequest) AddHeader

func (r *HTTPRequest) AddHeader(name, value string)

Adds a header that will be sent with the HTTP request.

func (*HTTPRequest) AddParameter

func (r *HTTPRequest) AddParameter(name, value string)

Adds a parameter to the generated query string.

func (*HTTPRequest) GetResponseFromJSON

func (r *HTTPRequest) GetResponseFromJSON(v interface{}) error

func (*HTTPRequest) MakeDeleteRequest

func (r *HTTPRequest) MakeDeleteRequest() (*HTTPResponse, error)

func (*HTTPRequest) MakeGetRequest

func (r *HTTPRequest) MakeGetRequest() (*HTTPResponse, error)

func (*HTTPRequest) MakePostRequest

func (r *HTTPRequest) MakePostRequest(payload Payload) (*HTTPResponse, error)

func (*HTTPRequest) MakePutRequest

func (r *HTTPRequest) MakePutRequest(payload Payload) (*HTTPResponse, error)

func (*HTTPRequest) MakeRequest

func (r *HTTPRequest) MakeRequest(method string, payload Payload) (*HTTPResponse, error)

func (*HTTPRequest) PostResponseFromJSON

func (r *HTTPRequest) PostResponseFromJSON(payload Payload, v interface{}) error

func (*HTTPRequest) SetBasicAuth

func (r *HTTPRequest) SetBasicAuth(user, password string)

Sets username and password for basic authentication.

func (*HTTPRequest) SetClient

func (r *HTTPRequest) SetClient(c *http.Client)

type HTTPResponse

type HTTPResponse struct {
	Code int
	Data []byte
}

func (*HTTPResponse) ParseFromJSON

func (r *HTTPResponse) ParseFromJSON(v interface{}) error

Parses the HTTPResponse as JSON to the given interface.

func (*HTTPResponse) ParseFromXML

func (r *HTTPResponse) ParseFromXML(v interface{}) error

type Payload

type Payload interface {
	GetPayloadBuffer() (*bytes.Buffer, error)
	GetContentType() string
}

type RawPayload

type RawPayload struct {
	Data []byte
}

func NewRawPayload

func NewRawPayload(data []byte) *RawPayload

func (*RawPayload) GetContentType

func (r *RawPayload) GetContentType() string

func (*RawPayload) GetPayloadBuffer

func (r *RawPayload) GetPayloadBuffer() (*bytes.Buffer, error)

type Request

type Request struct {
	Url            string
	Authentication BasicAuthentication
	UserAgent      string
	Data           []byte
}

Type to wrap requests.

func (Request) Delete

func (r Request) Delete() (int, []byte, error)

func (Request) Get

func (r Request) Get() (int, []byte, error)

func (Request) Post

func (r Request) Post() (int, []byte, error)

func (Request) Put

func (r Request) Put() (int, []byte, error)

type UrlEncodedPayload

type UrlEncodedPayload struct {
	Values []keyValuePair
}

func NewUrlEncodedPayload

func NewUrlEncodedPayload() *UrlEncodedPayload

func (*UrlEncodedPayload) AddValue

func (f *UrlEncodedPayload) AddValue(key, value string)

func (*UrlEncodedPayload) GetContentType

func (f *UrlEncodedPayload) GetContentType() string

func (*UrlEncodedPayload) GetPayloadBuffer

func (f *UrlEncodedPayload) GetPayloadBuffer() (*bytes.Buffer, error)

Jump to

Keyboard shortcuts

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