Skip to content

xioxu/goreq

Repository files navigation

goreq

Build Status GoDoc LICENSE

A simple http request library for golang.

Install

go get github.com/xioxu/goreq

Simple to use

    req := goreq.Req(nil)
	body,_,_ := req.Get("https://www.baidu.com").Do()
	fmt.Print(string(body))

Table of contents

Options

Property Description Notes
Method HTTP method -
URL Fully qualified uri -
Proxy Address of an HTTP proxy -
Headers HTTP headers to setup for the request -
FollowRedirect Follow HTTP 3xx responses as redirects default: true , you can use goreq.TrueVal or goreq.FalseVal to set
DisableKeepAlive Disable keep alive feature default: false, you can use goreq.TrueVal or goreq.FalseVal to set
Jar A cookie Jar to use for multiple subsequent requests You can define your own custom cookie jar; see cookies section in following
QueryString Object containing querystring values to be appended to the uri -
Timeout Request timeout value Global request timeout(e.g: 15 * time.Second), see https://blog.cloudflare.com/the-complete-guide-to-golang-net-http-timeouts/#clienttimeouts
HeadersToBeRemove The headers you want to remove before send request -

Proxy

If you specify a proxy option, then the request (and any subsequent redirects) will be sent via a connection to the proxy server.

    req := goreq.Req(&goreq.ReqOptions{ Proxy: goreq.NewString("http://localhost:8888")})
	body,resp,_ := req.Get("https://www.baidu.com").Do()
	fmt.Println(string(body))
	fmt.Println(resp.StatusCode)

Pipe

There are some Pipe methods to handle different case:

PipeStream

You can pipe any response to a writer. (Refer to UT: TestPipeSream)

  req := goreq.Req(nil)
  req.Get("https://www.baidu.com").PipeStream(fileStream)

PipeReq

You can pipe a request result to next request (Refer to UT: TestPipeReq)

  req1 := goreq.Req(nil)
  req2 := goreq.Req(nil)
  req2.Post("http://www.bbb.com/submit")
  req.Get("https://www.baidu.com").PipeReq(req2)

PipeFromReq

Pipe the http.Request content to goReq request (UT: TestPipeFromReq)

PipeToResponse

Pipe the result to a http.Response

We can create a reverseProxy server through PipeFromReq and PipeToResponse easily:

if err := http.ListenAndServe(":8080", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

		req := goreq.Req(&goreq.ReqOptions{
			Method: r.Method,
			Url:    "https://www.baidu.com" + r.RequestURI,
		})

		req.PipeFromReq(r).PipeToResponse(w)
	})); err != nil {
		panic(err)
	}

BodyContent

You can set the request body with the folloing methods:

  • JsonObject
  • JsonString
  • FormData
	req := Req(nil)

	v := url.Values{
		"username": []string{"test"},
		"password": []string{"test"},
	}

	body,_, err := req.Post("http://localhost/login").FormData(v).Do()

	if err != nil{
		fmt.Print(body)
	}

Cookie

cookieJar,_ := cookiejar.New(nil)
req := goreq.Req(&goreq.ReqOptions{
			Method: "get",
			Url:    "https://www.baidu.com",
			Jar:cookieJar,
		})

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages