Documentation ¶
Overview ¶
Package fasthttp contains fasthttp specific utilities for handling compression, authorization, and more
Index ¶
Examples ¶
Constants ¶
const UserAgent = "Mozilla/5.0 (compatible; netutils/1.0; +https://github.com/xplorfin/netutils)"
UserAgent contains the netutils user agent
Variables ¶
var ( // AcceptEncoding is a byte encoded header for accepting encoding // used to speed up fasthttp requests. Defined here: https://git.io/JYTc2 AcceptEncoding = []byte(fasthttp.HeaderAcceptEncoding) // Gzip is the gzip header defined here https://git.io/JYTcg Gzip = []byte("gzip") // Authorization contains the authorization header Authorization = []byte(fasthttp.HeaderAuthorization) // ContentEncoding is the ContentEncoding = []byte(fasthttp.HeaderContentEncoding) ContentType = []byte(fasthttp.HeaderContentType) // Brotli is the long brotli header Brotli = []byte("brotli") // BrotliShort is the abbreviated brotli header BrotliShort = []byte("br") // JSONEncoding is the header for when data is json encoded JSONEncoding = []byte("application/json") // GzipBrotli is the header for data encoded with both gzip and brotli GzipBrotli = []byte("gzip, brotli") )
Byte encoded headers for convience
var UserAgentBytes = []byte(UserAgent)
UserAgentBytes contains the raw user agent encoded as a byte slice
Functions ¶
func GetRawURL ¶
GetRawURL gets a rawurl using fasthttp
Example ¶
gets a byte slice url using fasthttp
// fetch example.org res, err := fasthttpHelper.GetRawURL([]byte("https://api.entropy.rocks")) if err != nil { panic(err) } // print the response body fmt.Println(string(res))
Output:
func GetURL ¶
GetURL gets a string url and unzips body. This is mostly a utility function
Example ¶
gets a byte slice url using fasthttp
// fetch example.org res, err := fasthttpHelper.GetURL("https://api.entropy.rocks") if err != nil { panic(err) } // print the response body fmt.Println(string(res))
Output:
Types ¶
type FastClient ¶
type FastClient struct { //modify a fast http request before it's send ModifyRequest ModifyRequestFunc // process a response before it's received ProcessResponse ProcessResponseFunc // user agent to use UserAgent string }
FastClient represents a client used for requests
func NewFastClient ¶
func NewFastClient() FastClient
NewFastClient creates a fast client with a user agent this can be changed if needed in ModifyRequest
Example ¶
// create a new fast client fastClient := fasthttpHelper.NewFastClient() fastClient.ModifyRequest = func(request *fasthttp.Request) { // use a custom user agent request.Header.SetUserAgent(browser.Android()) } // since responses are dropped after use in fast http this processes them in method fastClient.ProcessResponse = func(response *fasthttp.Response) { // print the headers fmt.Println(string(response.Header.Header())) } resp, err := fastClient.Request("https://api.entropy.rocks/") if err != nil { panic(err) } // get the response after it's been unzipped fmt.Println(string(resp))
Output:
type ModifyRequestFunc ¶
ModifyRequestFunc represents a function that allows a request to be modified before it is set
type ProcessResponseFunc ¶
ProcessResponseFunc represents a function that allows a response to be modified before it is set