HttpRequest

package module
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2019 License: Apache-2.0 Imports: 14 Imported by: 35

README

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()
req := HttpRequest.NewRequest().Debug(true).DisableKeepAlives(false).SetTimeout(5)

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",
    "Connection": "keep-alive",
})

req.SetHeaders(map[string]string{
    "Source":"api",
})

Set cookies

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

req.SetCookies(map[string]string{
    "age":"19",
})

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")
GET

Query parameter

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

res, err := HttpRequest.Get("http://127.0.0.1:8000")
res, err := HttpRequest.Debug(true).SetHeaders(map[string]string{}).Get("http://127.0.0.1:8000")

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")
res, err := req.Post("http://127.0.0.1:8000", "title=github&type=1")
res, err := req.JSON().Post("http://127.0.0.1:8000", "{\"id\":10,\"title\":\"HttpRequest\"}")
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)

res, err := HttpRequest.Post("http://127.0.0.1:8000")
res, err := HttpRequest.JSON().Post("http://127.0.0.1:8000",map[string]interface{}{"title":"github"})
res, err := HttpRequest.Debug(true).SetHeaders(map[string]string{}).JSON().Post("http://127.0.0.1:8000","{\"title\":\"github\"}")
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
ReqBody: map[age:19 score:100]
-------------------------------------------------------------------

Json

Post JSON request

Set header

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

Or

req.JSON().Post("http://127.0.0.1:8000", map[string]interface{}{
    "id":    10,
    "title": "github",
})

req.JSON().Post("http://127.0.0.1:8000", "{\"title\":\"github\",\"id\":10}")

Post request

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

Print formatted JSON

str, err := res.Export()
if err != nil {
   return
}

Unmarshal JSON

var u User
err := res.Json(&u)
if err != nil {
   return err
}

var m map[string]interface{}
err := res.Json(&m)
if err != nil {
   return err
}
Response

Response() *http.Response

res, err := req.Post("http://127.0.0.1:8000/") //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

Print formatted JSON

str, err := res.Export()
if err != nil {
   return
}

Unmarshal JSON

var u User
err := res.Json(&u)
if err != nil {
   return err
}

var m map[string]interface{}
err := res.Json(&m)
if err != nil {
   return err
}

Url() string

res.Url()  //return the requested url

Headers() map[string]string

res.Headers()  //return the response headers
Advanced

GET

import "github.com/kirinlabs/HttpRequest"
   
res,err := HttpRequest.Get("http://127.0.0.1:8000/")
res,err := HttpRequest.Get("http://127.0.0.1:8000/","title=github")
res,err := HttpRequest.Get("http://127.0.0.1:8000/?title=github")
res,err := HttpRequest.Debug(true).JSON().Get("http://127.0.0.1:8000/")

POST

import "github.com/kirinlabs/HttpRequest"
   
res,err := HttpRequest.Post("http://127.0.0.1:8000/")
res,err := HttpRequest.SetHeaders(map[string]string{
	"title":"github",
}).Post("http://127.0.0.1:8000/")
res,err := HttpRequest.Debug(true).JSON().Post("http://127.0.0.1:8000/")
Example
import "github.com/kirinlabs/HttpRequest"
   
res,err := HttpRequest.Get("http://127.0.0.1:8000/")
res,err := HttpRequest.Get("http://127.0.0.1:8000/","title=github")
res,err := HttpRequest.Get("http://127.0.0.1:8000/?title=github")
res,err := HttpRequest.Get("http://127.0.0.1:8000/",map[string]interface{}{
	"title":"github",
})
res,err := HttpRequest.Debug(true).JSON().SetHeaders(map[string]string{
	"source":"api",
}).SetCookies(map[string]string{
	"name":"httprequest",
}).Post("http://127.0.0.1:8000/")


//Or
req := HttpRequest.NewRequest()
req := req.Debug(true).SetHeaders()
res,err := req.Debug(true).JSON().SetHeaders(map[string]string{
    "source":"api",
}).SetCookies(map[string]string{
    "name":"httprequest",
}).Post("http://127.0.0.1:8000/")

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Export

func Export(v interface{}) string

func Json

func Json(v interface{}) string

Types

type Request

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

func Debug added in v0.1.4

func Debug(v bool) *Request

Debug model

func DisableKeepAlives added in v0.1.4

func DisableKeepAlives(v bool) *Request

func JSON added in v0.1.4

func JSON() *Request

func NewRequest

func NewRequest() *Request

func SetCookies added in v0.1.4

func SetCookies(cookies map[string]string) *Request

func SetHeaders added in v0.1.4

func SetHeaders(headers map[string]string) *Request

func SetTLSClient added in v0.1.4

func SetTLSClient(v *tls.Config) *Request

func SetTimeout added in v0.1.4

func SetTimeout(d time.Duration) *Request

func (*Request) Debug

func (r *Request) Debug(v bool) *Request

Debug model

func (*Request) Delete

func (r *Request) Delete(url string, data ...interface{}) (*Response, error)

Delete is a delete http request

func (*Request) DisableKeepAlives

func (r *Request) DisableKeepAlives(v bool) *Request

func (*Request) Get

func (r *Request) Get(url string, data ...interface{}) (*Response, error)

Get is a get http request

func (*Request) JSON added in v0.1.4

func (r *Request) JSON() *Request

func (*Request) Post

func (r *Request) Post(url string, data ...interface{}) (*Response, error)

Post is a post http request

func (*Request) Put

func (r *Request) Put(url string, data ...interface{}) (*Response, error)

Put is a put http request

func (*Request) SetCookies

func (r *Request) SetCookies(cookies map[string]string) *Request

Set cookies

func (*Request) SetHeaders

func (r *Request) SetHeaders(headers map[string]string) *Request

Set headers

func (*Request) SetTLSClient

func (r *Request) SetTLSClient(v *tls.Config) *Request

func (*Request) SetTimeout

func (r *Request) SetTimeout(d time.Duration) *Request

func (*Request) Upload

func (r *Request) Upload(url, filename, fileinput string) (*Response, error)

Upload file

type Response

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

func Delete added in v0.1.4

func Delete(url string, data ...interface{}) (*Response, error)

Delete is a delete http request

func Get added in v0.1.4

func Get(url string, data ...interface{}) (*Response, error)

Get is a get http request

func Post added in v0.1.4

func Post(url string, data ...interface{}) (*Response, error)

func Put added in v0.1.4

func Put(url string, data ...interface{}) (*Response, error)

Put is a put http request

func Upload added in v0.1.4

func Upload(url, filename, fileinput string) (*Response, error)

Upload file

func (*Response) Body

func (r *Response) Body() ([]byte, error)

func (*Response) Export added in v0.1.5

func (r *Response) Export() (string, error)

func (*Response) Headers added in v0.1.4

func (r *Response) Headers() map[string]string

func (*Response) Json

func (r *Response) Json(v interface{}) error

func (*Response) Response

func (r *Response) Response() *http.Response

func (*Response) StatusCode

func (r *Response) StatusCode() int

func (*Response) Time

func (r *Response) Time() string

func (*Response) Url

func (r *Response) Url() string

Jump to

Keyboard shortcuts

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