ddo / request

:pizza: Simplified HTTP request client in go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

request Build Status Doc

Simplified HTTP request client in go

installation

go get gopkg.in/ddo/request.v1

option

  • 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()
data, res, err := client.Request(&request.Option{
    URL: "https://httpbin.org/get",
})
if err != nil {
    panic(err)
}

POST

data, 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

data, 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

data, 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

DLOG=*

or

DEBUG=* go run file.go

test

go test -v

TODO

  • default settings
  • hooks
  • file

About

:pizza: Simplified HTTP request client in go

License:MIT License


Languages

Language:Go 100.0%