request

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2015 License: MIT Imports: 8 Imported by: 0

README

request Build Status Doc

Simplified HTTP request client in go

##Options

  • Url string required
  • Method string default: "GET", anything "POST", "PUT", "DELETE" or "PATCH"
  • BodyStr string
  • Body *Data
  • Form *Data set Content-Type header as "application/x-www-form-urlencoded"
  • Json interface{} set Content-Type header as "application/json"
  • Query *Data
  • Header *Header

###GET

client := request.New()

body, res, err := client.Request(&request.Option{
    Url: "https://httpbin.org/get",
})

if err != nil {
    panic(err)
}

fmt.Println(res)
fmt.Println(body)

###POST

body, res, err := client.Request(&request.Option{
    Url:    "https://httpbin.org/post",
    Method: "POST",
    Body: &request.Data{
        "two":   []string{"2", "hai"},
        "three": []string{"3", "ba", "trois"},
        "email": []string{"ddo@ddo.me"},
    },
})

###POST form

body, res, err := client.Request(&request.Option{
    Url:    "https://httpbin.org/post",
    Method: "POST",
    Form: &request.Data{
        "two":   []string{"2", "hai"},
        "three": []string{"3", "ba", "trois"},
        "email": []string{"ddo@ddo.me"},
    },
})

###Json

body, res, err := client.Request(&request.Option{
    Url:    "https://httpbin.org/post",
    Method: "POST",
    Json: map[string]interface{}{
        "int":    1,
        "string": "two",
        "array":  []string{"3", "ba", "trois"},
        "object": map[string]interface{}{
            "int": 4,
        },
    },
})

##Logger

to enable log set environment variable as

DEBUG=request

or

DEBUG=request go run file.go

##Test

go test -v

##TODO

  • default settings
  • hooks
  • file

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

func New

func New() *Client

func (*Client) GetCookie

func (c *Client) GetCookie(domain, name string) (value string, err error)

func (*Client) Request

func (c *Client) Request(opt *Option) (body string, res *http.Response, err error)

type Data

type Data map[string][]string
type Header map[string]string

type Option

type Option struct {
	Url     string //required
	Method  string //default: "GET", anything "POST", "PUT", "DELETE" or "PATCH"
	BodyStr string
	Body    *Data
	Form    *Data       //set Content-Type header as "application/x-www-form-urlencoded"
	Json    interface{} //set Content-Type header as "application/json"
	Query   *Data
	Header  *Header
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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