client

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2018 License: MIT Imports: 16 Imported by: 0

README

gowww client GoDoc Build Coverage Go Report Status Testing

Package client provides an HTTP client for clean requests.

Installing

  1. Get package:

    go get -u github.com/gowww/client
    
  2. Import it in your code:

    import "github.com/gowww/client"
    

Usage

Request

Use Get, Post, Put, Patch, Delete or Head with the destination URL to initiate a request.
Options are chainable:

file, err := os.Open("data/one.txt")
if err != nil {
	panic(err)
}
defer file.Close()

req := client.Post("http://example.com").
	DisableRedirect().
	ForceMultipart().
	Header("Accept-Language", "en").
	UserAgent("Googlebot/2.1 (+http://www.google.com/bot.html)").
	Cookie(&http.Cookie{Name: "session", Value: "123"}).
	Value("id", "123").
	Value("name", "Doe").
	File("file", "one.txt", file).
	OpenFile("picture", "one.png").
	OpenFile("picture", "two.png")

Finally, use Do to send the requet and get the response or, eventually, the deferred first error of procedure:

res, err := req.Do()
if err != nil {
	panic(err)
}
defer res.Close()

Don't forget to close the response body when done.

Response

A Response wraps the standard http.Response and provides some utility functions.

Use Response.Cookie to retrieve a single cookie:

c, err := res.Cookie(tokenCookieName)
if err != nil {
	// Generally, error is http.ErrNoCookie.
}

Use Response.JSON to decode a JSON body into a variable:

jsres := new(struct{
	ID string `json:"id"`
})
res.JSON(jsres)
Debugging

Debugging a request is pretty simple with Response.Dump.
It prints the request info, writes the body in a file and opens it in your browser.

res, err := client.Get("http://example.com").Do()
if err != nil {
	panic(err)
}
res.Dump()

Documentation

Overview

Package client provides an HTTP client for clean requests.

Package client provides cient request itilities.

Example
package main

import (
	"fmt"
	"net/http"
	"os"

	"github.com/gowww/client"
)

func main() {
	file, err := os.Open("data/one.txt")
	if err != nil {
		panic(err)
	}
	defer file.Close()

	req := client.Post("http://example.com").
		DisableRedirect().
		ForceMultipart().
		Header("Accept-Language", "en").
		UserAgent("Googlebot/2.1 (+http://www.google.com/bot.html)").
		Cookie(&http.Cookie{Name: "session", Value: "123"}).
		Value("id", "123").
		Value("name", "Doe").
		File("file", "one.txt", file).
		OpenFile("picture", "one.png").
		OpenFile("picture", "two.png")

	res, err := req.Do()
	if err != nil {
		panic(err)
	}
	defer res.Close()

	fmt.Println(res)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Timeout

func Timeout(t time.Duration)

Timeout sets the global client request timeout. Default is 30 seconds.

Types

type Request

type Request interface {
	Value(key, value string) Request
	File(key, filename string, file io.Reader) Request
	OpenFile(key, filepath string) Request
	Header(key, value string) Request
	Cookie(*http.Cookie) Request
	UserAgent(value string) Request
	DisableRedirect() Request
	ForceMultipart() Request
	Do() (*Response, error)
	String() string
}

Request is a client request.

func Delete

func Delete(url string) Request

Delete makes a DELETE request.

func Get

func Get(url string) Request

Get makes a GET request.

func Head(url string) Request

Head makes a HEAD request.

func New

func New(method, url string) Request

New makes a new request for method and URL.

func Patch

func Patch(url string) Request

Patch makes a PATCH request.

func Post

func Post(url string) Request

Post makes a POST request.

func Put

func Put(url string) Request

Put makes a PUT request.

type Response

type Response struct {
	*http.Response
}

Response is a request response wrapping the original *http.Request.

func (*Response) BodyBytes

func (r *Response) BodyBytes() ([]byte, error)

BodyBytes returns the response body as bytes.

func (*Response) BodyString

func (r *Response) BodyString() (string, error)

BodyString returns the response body as a string.

func (*Response) Close

func (r *Response) Close() error

Close closes the response body. It must be called after body is no longer used.

func (*Response) Cookie

func (r *Response) Cookie(name string) (*http.Cookie, error)

Cookie returns the named cookie provided in the request or http.ErrNoCookie if not found. If multiple cookies match the given name, only one cookie will be returned.

func (*Response) Dump

func (r *Response) Dump()

Dump is for debug purpose. It prints the request info, writes the body in a file and opens it in the browser. It panics on error.

func (*Response) JSON

func (r *Response) JSON(v interface{}) error

JSON decodes a JSON body into v.

func (*Response) Path

func (r *Response) Path() string

Path returns the final response path.

func (*Response) String

func (r *Response) String() string

Jump to

Keyboard shortcuts

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