Categorygithub.com/levigross/grequests
modulepackage
0.0.0-20231203190023-9c307ef1f48d
Repository: https://github.com/levigross/grequests.git
Documentation: pkg.go.dev

# README

GRequests

A Go "clone" of the great and famous Requests library

Join the chat at https://gitter.im/levigross/grequests

License

GRequests is licensed under the Apache License, Version 2.0. See LICENSE for the full license text

Features

  • Responses can be serialized into JSON and XML
  • Easy file uploads
  • Easy file downloads
  • Support for the following HTTP verbs GET, HEAD, POST, PUT, DELETE, PATCH, OPTIONS

Install

go get -u github.com/levigross/grequests

Usage

import "github.com/levigross/grequests"

Basic Examples

Basic GET request:

resp, err := grequests.Get("http://httpbin.org/get", nil)
// You can modify the request by passing an optional RequestOptions struct

if err != nil {
	log.Fatalln("Unable to make request: ", err)
}

fmt.Println(resp.String())
// {
//   "args": {},
//   "headers": {
//     "Accept": "*/*",
//     "Host": "httpbin.org",

If an error occurs all of the other properties and methods of a Response will be nil

Quirks

Request Quirks

When passing parameters to be added to a URL, if the URL has existing parameters that contradict with what has been passed within ParamsParams will be the "source of authority" and overwrite the contradicting URL parameter.

Lets see how it works...

ro := &RequestOptions{
	Params: map[string]string{"Hello": "Goodbye"},
}
Get("http://httpbin.org/get?Hello=World", ro)
// The URL is now http://httpbin.org/get?Hello=Goodbye

Response Quirks

Order matters! This is because grequests.Response is implemented as an io.ReadCloser which proxies the http.Response.Body io.ReadCloser interface. It also includes an internal buffer for use in Response.String() and Response.Bytes().

Here are a list of methods that consume the http.Response.Body io.ReadCloser interface.

  • Response.JSON
  • Response.XML
  • Response.DownloadToFile
  • Response.Close
  • Response.Read

The following methods make use of an internal byte buffer

  • Response.String
  • Response.Bytes

In the code below, once the file is downloaded – the Response struct no longer has access to the request bytes

response := Get("http://some-wonderful-file.txt", nil)

if err := response.DownloadToFile("randomFile"); err != nil {
	log.Println("Unable to download file: ", err)
}

// At this point the .String and .Bytes method will return empty responses

response.Bytes() == nil // true
response.String() == "" // true

But if we were to call response.Bytes() or response.String() first, every operation will succeed until the internal buffer is cleared:

response := Get("http://some-wonderful-file.txt", nil)

// This call to .Bytes caches the request bytes in an internal byte buffer – which can be used again and again until it is cleared
response.Bytes() == `file-bytes`
response.String() == "file-string"

// This will work because it will use the internal byte buffer
if err := resp.DownloadToFile("randomFile"); err != nil {
	log.Println("Unable to download file: ", err)
}

// Now if we clear the internal buffer....
response.ClearInternalBuffer()

// At this point the .String and .Bytes method will return empty responses

response.Bytes() == nil // true
response.String() == "" // true

# Functions

BuildHTTPClient is a function that will return a custom HTTP client based on the request options provided the check is in UseDefaultClient.
Delete takes 2 parameters and returns a Response struct.
DoRegularRequest adds generic test functionality.
EnsureTransporterFinalized will ensure that when the HTTP client is GCed the runtime will close the idle connections (so that they won't leak) this function was adopted from Hashicorp's go-cleanhttp package.
FileUploadFromDisk allows you to create a FileUpload struct slice by just specifying a location on the disk.
FileUploadFromGlob allows you to create a FileUpload struct slice by just specifying a glob location on the disk this function will gloss over all errors in the files and only upload the files that don't return errors from the glob.
Get takes 2 parameters and returns a Response Struct.
Head takes 2 parameters and returns a Response channel.
NewSession returns a session struct which enables can be used to maintain establish a persistent state with the server This function will set UseCookieJar to true as that is the purpose of using the session.
Options takes 2 parameters and returns a Response struct.
Patch takes 2 parameters and returns a Response struct.
Post
Post takes 2 parameters and returns a Response channel.
Put takes 2 parameters and returns a Response struct.
Req takes 3 parameters and returns a Response Struct.

# Variables

ErrRedirectLimitExceeded is the error returned when the request responded with too many redirects.
RedirectLimit is a tunable variable that specifies how many times we can redirect in response to a redirect.
SensitiveHTTPHeaders is a map of sensitive HTTP headers that a user doesn't want passed on a redirect.

# Structs

FileUpload is a struct that is used to specify the file that a User wishes to upload.
RequestOptions is the location that of where the data.
Response is what is returned to a user when they fire off a request.
Session allows a user to make use of persistent cookies in between HTTP requests.

# Type aliases

XMLCharDecoder is a helper type that takes a stream of bytes (not encoded in UTF-8) and returns a reader that encodes the bytes into UTF-8.