goreq: github.com/xioxu/goreq Index | Examples | Files

package goreq

import "github.com/xioxu/goreq"

Package goreq provides the ability to do http request with the simplest code

Example to Request a site

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

Example to submit a request

req := goreq.Req(nil)
postFormData := url.Values{}
postFormData.Add("userName", "nxu")
postFormData.Add("pwd", "111")

body,_,_ := req.Post("https://www.baidu.com").FormData(postFormData).Do()

This top-level package contains utility functions and data types that are used throughout the http requesting.



Package Files

doc.go goreq.go nullable.go


var FalseVal = &falseVal
var TrueVal = &trueVal

func NewString Uses

func NewString(str string) *string

type GoReq Uses

type GoReq struct {
    Options *ReqOptions
    // contains filtered or unexported fields

GoReq stores details that are required to interact with a http request and represents the methods to manipulates the request data.

Generally, you acquire a GoRep by calling goReq.Req() method.

GoReq is not thread safe, so do not use it in concurrency case, but you can reuse it to do different http request, and it is also the suggested usage

func Req Uses

func Req(options *ReqOptions) *GoReq

Req prepares a GoReq instance. A basic example of using this would be:

req := goreq.Req(nil)


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

func (*GoReq) Do Uses

func (req *GoReq) Do() ([]byte, *http.Response, error)

Do method sends the request and return the result

func (*GoReq) FormData Uses

func (req *GoReq) FormData(formData url.Values) *GoReq

FormData sets the http request body and makes the content-type to "application/x-www-form-urlencoded"

func (*GoReq) Get Uses

func (req *GoReq) Get(url string) *GoReq

Get indicates use get method and the get url, equals to:

req.Options.Url = url
req.Options.Method = "GET"

func (*GoReq) JsonObject Uses

func (req *GoReq) JsonObject(jsonObj interface{}) *GoReq

JsonObject sets the http request body and makes the content-type to "application/json"

func (*GoReq) JsonString Uses

func (req *GoReq) JsonString(jsonStr []byte) *GoReq

JsonString sets the http request body and makes the content-type to "application/json"

func (*GoReq) PipeFromHttpReq Uses

func (req *GoReq) PipeFromHttpReq(r *http.Request) *GoReq

PipeFromHttpReq pips the http.Request content to goReq request

func (*GoReq) PipeReq Uses

func (req *GoReq) PipeReq(nextReq *GoReq) (*GoReq, error)

PipeReq pipes the request result to next request


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

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

})); err != nil {

func (*GoReq) PipeStream Uses

func (req *GoReq) PipeStream(writer io.Writer) error

PipeStream pipes the response to a writer (e.g: file stream).

func (*GoReq) PipeToResponse Uses

func (req *GoReq) PipeToResponse(w http.ResponseWriter) error

PipeToResponse pipes the result to a http.Response

func (*GoReq) Post Uses

func (req *GoReq) Post(url string) *GoReq

Post indicates use post method and the post url, equals to:

req.Options.Url = url
req.Options.Method = "POST"

func (*GoReq) Req Uses

func (req *GoReq) Req(options *ReqOptions) *GoReq

Req returns a GoRep with a nother GoRep options


req := Req(&ReqOptions{Proxy: NewString("http://localhost:8888")})

// req1 still keeps the options of req
req1 := req.Req(nil)

func (*GoReq) UnmarshalJson Uses

func (req *GoReq) UnmarshalJson(result interface{}) (*http.Response, error)

UnmarshalJson unmarshals json result to a model

type ReqOptions Uses

type ReqOptions struct {
    //http method (default: "GET"), case insensitive
    Method string

    //fully qualified uri or a parsed url object from url.parse()
    Url string

    //http headers (default: {})
    Headers http.Header

    // follow HTTP 3xx responses as redirects (default: true).
    FollowRedirect *bool

    // disable keep alive feature (default: false)
    DisableKeepAlive *bool

    // if not nil, remember cookies for future use (or define your custom cookie jar; see examples section)
    Jar *cookiejar.Jar

    //an HTTP proxy url to be used
    Proxy *string

    // object containing querystring values to be appended to the uri
    QueryString url.Values

    // e.g 15 * time.Second
    Timeout time.Duration

    HeadersToBeRemove []string
    // contains filtered or unexported fields

ReqOptions stores information needed during http request

Package goreq imports 10 packages (graph). Updated 2018-08-01. Refresh now. Tools for package owners.