modulepackage
0.0.0-20210125130424-6a183ee47e65
Repository: https://github.com/lhmwzy/sreq.git
Documentation: pkg.go.dev
# README
sreq
sreq is a simple, user-friendly and concurrent safe HTTP request library for Go, inspired by Python requests.
Features
- Requests-style APIs.
- GET, POST, PUT, PATCH, DELETE, etc.
- Easy set query params, headers and cookies.
- Easy send form, JSON or multipart payload.
- Easy set basic authentication or bearer token.
- Easy set proxy.
- Easy set context.
- Retry policy.
- Automatic cookies management.
- Request and response interceptors.
- Easy decode responses, raw data, text representation and unmarshal the JSON-encoded data.
- Friendly debugging.
- Concurrent safe.
Install
go get -u github.com/winterssy/sreq
# Go Modules only
go get -u github.com/winterssy/sreq@master
Usage
import "github.com/winterssy/sreq"
Quick Start
The usages of sreq
are very similar to net/http
, you can switch from it to sreq
easily. For example, if your HTTP request code like this:
resp, err := http.Get("http://www.google.com")
Use sreq
you just need to change your code like this:
resp, err := sreq.Get("http://www.google.com").Raw()
You have two convenient ways to access the APIs of sreq
.
const (
url = "http://httpbin.org/get"
userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"
)
params := sreq.Params{
"k1": "v1",
"k2": "v2",
}
client := sreq.New()
// Go-style
req := sreq.
NewRequest("GET", url).
SetQuery(params).
SetUserAgent(userAgent)
err := client.
Do(req).
EnsureStatusOk().
Verbose(ioutil.Discard)
if err != nil {
panic(err)
}
// Requests-style (Recommended)
err = client.
Get(url,
sreq.WithQuery(params),
sreq.WithUserAgent(userAgent),
).
EnsureStatusOk().
Verbose(os.Stdout)
if err != nil {
panic(err)
}
// Output:
// > GET /get?k1=v1&k2=v2 HTTP/1.1
// > Host: httpbin.org
// > User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36
// >
// < HTTP/1.1 200 OK
// < Access-Control-Allow-Origin: *
// < Content-Type: application/json
// < Referrer-Policy: no-referrer-when-downgrade
// < Server: nginx
// < Access-Control-Allow-Credentials: true
// < Date: Mon, 02 Dec 2019 06:24:29 GMT
// < X-Content-Type-Options: nosniff
// < X-Frame-Options: DENY
// < X-Xss-Protection: 1; mode=block
// < Connection: keep-alive
// <
// {
// "args": {
// "k1": "v1",
// "k2": "v2"
// },
// "headers": {
// "Accept-Encoding": "gzip",
// "Host": "httpbin.org",
// "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"
// },
// "origin": "8.8.8.8, 8.8.8.8",
// "url": "https://httpbin.org/get?k1=v1&k2=v2"
// }
License
# Packages
No description provided by the author
# Functions
AppendClientCertificates appends client certificates to the HTTP client.
AppendRootCAs appends root certificate authorities to the HTTP client.
DefaultTransport returns an HTTP transport used by DefaultClient.
Delete makes a DELETE HTTP request.
DisableProxy makes the HTTP client not use proxy.
DisableRedirect makes the HTTP client not follow redirects.
DisableSession makes the HTTP client not use cookie jar.
DisableVerify makes the HTTP client not verify the server's TLS certificate.
Do sends a request and returns its response.
FilterCookie returns the named cookie to send in a request for the given URL.
FilterCookies returns the cookies to send in a request for the given URL.
Get makes a GET HTTP request.
Head makes a HEAD HTTP request.
MustOpen opens the named file and returns a *File instance whose Filename is filename.
New returns a new Client.
NewFile returns a *File instance given a filename and its body.
NewRequest returns a new Request given a method, URL.
Open opens the named file and returns a *File instance whose Filename is filename.
Patch makes a PATCH HTTP request.
Post makes a POST HTTP request.
Put makes a PUT HTTP request.
Send makes an HTTP request using a specified method.
SetCookieJar sets cookie jar of the HTTP client.
SetProxy sets proxy of the HTTP client.
SetProxyFromURL sets proxy of the HTTP client from a url.
SetRedirect sets policy of the HTTP client for handling redirects.
SetRetry sets retry policy of the client.
SetTimeout sets timeout of the HTTP client.
SetTLSClientConfig sets TLS configuration of the HTTP client.
SetTransport sets transport of the HTTP client.
UseRequestInterceptors appends request interceptors of the client.
UseResponseInterceptors appends response interceptors of the client.
WithBasicAuth sets basic authentication for the HTTP request.
WithBearerToken sets bearer token for the HTTP request.
WithBody sets body for the HTTP request.
WithContent sets bytes payload for the HTTP request.
WithContentType sets Content-Type header value for the HTTP request.
WithContext sets context for the HTTP request.
WithCookies appends cookies for the HTTP request.
WithForm sets form payload for the HTTP request.
WithHeaders sets headers for the HTTP request.
WithHost sets host for the HTTP request.
WithJSON sets JSON payload for the HTTP request.
WithMultipart sets multipart payload for the HTTP request.
WithQuery sets query params for the HTTP request.
WithReferer sets Referer header value for the HTTP request.
WithRetry sets retry policy for the HTTP request.
WithText sets plain text payload for the HTTP request.
WithTimeout sets timeout for the HTTP request.
WithUserAgent sets User-Agent header value for the HTTP request.
WithXML sets XML payload for the HTTP request.
# Constants
DefaultTimeout is the timeout used by DefaultClient.
MethodConnect represents the CONNECT method for HTTP.
MethodDelete represents the DELETE method for HTTP.
MethodGet represents the GET method for HTTP.
MethodHead represents the HEAD method for HTTP.
MethodOptions represents the OPTIONS method for HTTP.
MethodPatch represents the PATCH method for HTTP.
MethodPost represents the POST method for HTTP.
MethodPut represents the PUT method for HTTP.
MethodTrace represents the TRACE method for HTTP.
Version of sreq.
# Variables
DefaultClient is the default sreq Client, used for the global functions such as Get, Post, etc.
ErrJarCookiesNotPresent can be used when cookies for a given URL not present in cookie jar.
ErrJarNamedCookieNotPresent can be used when named cookie for a given URL not present in cookie jar.
ErrNilContext can be used when the context is nil.
ErrNilCookieJar can be used when the cookie jar is nil.
ErrResponseCookiesNotPresent can be used when cookies of the HTTP response not present.
ErrResponseNamedCookieNotPresent can be used when named cookie of the HTTP response not present.
ErrUnexpectedTransport can be used if assert a RoundTripper as a non-nil *http.Transport instance failed.
# Structs
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
# Interfaces
No description provided by the author
# Type aliases
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author