itsky365 / HttpRequest

Go curl : A simple HTTP Request package for golang.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

HttpRequest

A simple HTTP Request package for golang. GET POST DELETE PUT Upload

Installation

go get github.com/kirinlabs/HttpRequest

How do we use HttpRequest?

Create request object

req := HttpRequest.NewRequest()

Keep Alives

req.DisableKeepAlives(false)

Ignore Https certificate validation

req.SetTLSClient(&tls.Config{InsecureSkipVerify: true})

Set headers

req.SetHeaders(map[string]string{
    "Content-Type": "application/x-www-form-urlencoded",
})

Set cookies

req.SetCookies(map[string]string{
    "name":"json",
})

Set timeout

req.SetTimeout(5)  //default 30s

Object-oriented operation mode

req := HttpRequest.NewRequest().Debug(true).SetHeaders(map[string]string{
    "Content-Type": "application/x-www-form-urlencoded",
}).SetTimeout(5)
res,err := HttpRequest.NewRequest().Get("http://127.0.0.1:8000?id=10&title=HttpRequest",nil)

GET

Query parameter

res, err := req.Get("http://127.0.0.1:8000?id=10&title=HttpRequest",nil)

Multi parameter,url will be rebuild to http://127.0.0.1:8000?id=10&title=HttpRequest&name=jason&score=100

res, err := req.Get("http://127.0.0.1:8000?id=10&title=HttpRequest",map[string]interface{}{
    "name":  "jason",
    "score": 100,
})
body, err := res.Body()
if err != nil {
    return
}
return string(body)

POST

res, err := req.Post("http://127.0.0.1:8000", map[string]interface{}{
    "id":    10,
    "title": "HttpRequest",
})
body, err := res.Body()
if err != nil {
    return
}
return string(body)

Upload

Params: url, filename, fileinput

res, err := req.Upload("http://127.0.0.1:8000/upload", "/root/demo.txt","uploadFile")
body, err := res.Body()
if err != nil {
    return
}
return string(body)

Debug

Default false

req.Debug(true)

Print in standard output:

[HttpRequest]
-------------------------------------------------------------------
Request: GET http://127.0.0.1:8000?name=iceview&age=19&score=100
Headers: map[Content-Type:application/x-www-form-urlencoded]
Cookies: map[]
Timeout: 30s
BodyMap: map[age:19 score:100]
-------------------------------------------------------------------

Json

Post JSON request

Set header

 req.SetHeaders(map[string]string{"Content-Type": "application/json"})

Post request

res, err := req.Post("http://127.0.0.1:8000", map[string]interface{}{
    "id":    10,
    "title": "HttpRequest",
})

Print JSON

body, err := res.Json()
if err != nil {
   return
}

Response

Response() *http.Response

res, err := req.Post("http://127.0.0.1:8000/",nil) //res is a http.Response object

StatusCode() int

res.StatusCode()

Body() ([]byte, error)

body, err := res.Body()
log.Println(string(body))

Time() string

res.Time()  //ms

Json() (string,error)

body, err := res.Json() //Format the json return value
log.Println(body)

Url() string

res.Url()  //return the requested url

About

Go curl : A simple HTTP Request package for golang.

License:Apache License 2.0


Languages

Language:Go 100.0%