request

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