httpwrapper
HTTP client wrapper using h2non/gentleman
Quick Usage
package main
import (
"time"
// automatically set debug flag (output HTTP request/response to stdout)
// _ "github.com/evalphobia/httpwrapper/debug"
"github.com/evalphobia/httpwrapper/request"
)
func main() {
// Example #1
// GET request
resp, err := request.GET("http://example.com", request.Option{
Headers: map[string]string{"x-example-token": "abcdefg"},
Timeout: 10 * time.Second,
Retry: true,
// output HTTP request/response
Debug: false,
// for basic auth
User: "MyName",
Pass: "secret",
Query: "name=MyName&pass=secret",
// Query: map[string]string{"name": "MyName", "pass": "secret"},
})
if err != nil {
panic(err)
}
// if status code is not 2xx.
if err := resp.HasStatusCodeError(); err != nil {
panic(err)
}
// for JSON response
user := User{}
err = resp.JSON(&user)
if err != nil {
panic(err)
}
// for XML response
err = resp.XML(&user, nil)
if err != nil {
panic(err)
}
// Example #2
// POST request and set response result.
user = User{}
err = request.CallWithResult(request.Option{
URL: "http://example.com",
Method: request.MethodPOST,
PayloadType: request.PayloadTypeJSON,
Payload: map[string]interface{}{"param1": 100, "param2": "abc"},
}, &user)
if err != nil {
panic(err)
}
// ...
}
// struct for HTTP response data
type User struct {
LastName string `json:"last_name" xml:"last_name"`
FirstName string `json:"first_name" xml:"first_name"`
Age int `json:"age" xml:"age"`
}
Option
Name |
Description |
Example |
URL |
Request URL |
http://exmaple.com |
Method |
HTTP method name |
GET , POST , PUT , DELETE . request package has constants (request.MethodGET and others) |
Headers |
HTTP headers |
map[string]string{"X-HTTP-SOMETHING": "1"} |
Timeout |
HTTP timeout |
30 * time.Second |
Retry |
retry flag, if set true and timeout has occured, then it tries another 2 requests |
|
Debug |
if set true , then it outputs HTTP request/response to stdout. |
|
Plugins |
gentleman's plugins |
|
UserAgent |
User Agent |
Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1 |
User |
Basic auth username |
|
Pass |
Basic auth password |
|
Query |
HTTP Query String in string , map[string]string or map[string]interface{} |
name=aaa&pass=bbb¶m=ccc |
PayloadType |
Define type of Payload |
JSON , XML , BODY , FORM , DATA . request package has variables (request.PayloadTypeJSON and others) |
Payload |
Request body parameters |
|