Documentation
¶
Overview ¶
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() fmt.Print(string(body))
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() fmt.Print(string(body))
This top-level package contains utility functions and data types that are used throughout the http requesting.
Index ¶
- Variables
- func NewString(str string) *string
- type GoReq
- func (req *GoReq) Do() ([]byte, *http.Response, error)
- func (req *GoReq) FormData(formData url.Values) *GoReq
- func (req *GoReq) Get(url string) *GoReq
- func (req *GoReq) JsonObject(jsonObj interface{}) *GoReq
- func (req *GoReq) JsonString(jsonStr []byte) *GoReq
- func (req *GoReq) PipeFromHttpReq(r *http.Request) *GoReq
- func (req *GoReq) PipeReq(nextReq *GoReq) (*GoReq, error)
- func (req *GoReq) PipeStream(writer io.Writer) error
- func (req *GoReq) PipeToResponse(w http.ResponseWriter) error
- func (req *GoReq) Post(url string) *GoReq
- func (req *GoReq) Req(options *ReqOptions) *GoReq
- func (req *GoReq) UnmarshalJson(result interface{}) (*http.Response, error)
- type ReqOptions
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var FalseVal = &falseVal
var TrueVal = &trueVal
Functions ¶
Types ¶
type GoReq ¶
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 ¶
func Req(options *ReqOptions) *GoReq
Req prepares a GoReq instance. A basic example of using this would be:
req := goreq.Req(nil)
Example ¶
req := Req(nil) body, _, _ := req.Get("https://www.baidu.com").Do() fmt.Print(string(body))
Output:
func (*GoReq) FormData ¶
FormData sets the http request body and makes the content-type to "application/x-www-form-urlencoded"
func (*GoReq) Get ¶
Get indicates use get method and the get url, equals to:
req.Options.Url = url req.Options.Method = "GET"
func (*GoReq) JsonObject ¶
JsonObject sets the http request body and makes the content-type to "application/json"
func (*GoReq) JsonString ¶
JsonString sets the http request body and makes the content-type to "application/json"
func (*GoReq) PipeFromHttpReq ¶
PipeFromHttpReq pips the http.Request content to goReq request
func (*GoReq) PipeReq ¶
PipeReq pipes the request result to next request
Example ¶
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, }) req.PipeFromHttpReq(r).PipeToResponse(w) })); err != nil { panic(err) }
Output:
func (*GoReq) PipeStream ¶
PipeStream pipes the response to a writer (e.g: file stream).
func (*GoReq) PipeToResponse ¶
func (req *GoReq) PipeToResponse(w http.ResponseWriter) error
PipeToResponse pipes the result to a http.Response
func (*GoReq) Post ¶
Post indicates use post method and the post url, equals to:
req.Options.Url = url req.Options.Method = "POST"
func (*GoReq) Req ¶
func (req *GoReq) Req(options *ReqOptions) *GoReq
Req returns a GoRep with a nother GoRep options
Example ¶
req := Req(&ReqOptions{Proxy: NewString("http://localhost:8888")}) // req1 still keeps the options of req req1 := req.Req(nil) req1.Get("http://www.baidu.com").Do()
Output:
type ReqOptions ¶
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