Skip to content

reqvshttr

MIT 46 1 4,126
58.1 thousand (month) Jun 28 2023 v3.43.7(a month ago)
982 9 2 MIT
May 06 2012 712.9 thousand (month) 1.4.7(1 year, 2 months ago)

The Go library "req" is a simple and easy-to-use library for making HTTP requests in Go. It is designed to make working with HTTP requests as simple as possible, by providing a clean and consistent API for handling various types of requests, including GET, POST, PUT, and DELETE.

One of the key features of req is its support for handling JSON data. The library automatically serializes and deserializes JSON data, making it easy to work with JSON data in your Go applications. Additionally, it supports multipart file uploads and automatic decompression of gzip and deflate encoded responses.

req also includes a number of convenience functions for working with common HTTP request types, such as sending GET and POST requests, handling redirects, and setting headers and query parameters. The library can also be easily extended with custom middleware and request handlers.

Overall, req is a powerful and flexible library that makes it easy to work with HTTP requests in Go. It is well-documented and actively maintained, making it a great choice for any Go project that needs to work with HTTP requests.

The aim of httr is to provide a wrapper for the curl package, customised to the demands of modern web APIs.

Key features:

  • Functions for the most important http verbs: GET(), HEAD(), PATCH(), PUT(), DELETE() and POST().
  • Automatic connection sharing across requests to the same website (by default, curl handles are managed automatically), cookies are maintained across requests, and a up-to-date root-level SSL certificate store is used.
  • Requests return a standard reponse object that captures the http status line, headers and body, along with other useful information.
  • Response content is available with content() as a raw vector (as = "raw"), a character vector (as = "text"), or parsed into an R object (as = "parsed"), currently for html, xml, json, png and jpeg.
  • You can convert http errors into R errors with stop_for_status().
  • Config functions make it easier to modify the request in common ways: set_cookies(), add_headers(), authenticate(), use_proxy(), verbose(), timeout(), content_type(), accept(), progress().
  • Support for OAuth 1.0 and 2.0 with oauth1.0_token() and oauth2.0_token(). The demo directory has eight OAuth demos: four for 1.0 (twitter, vimeo, withings and yahoo) and four for 2.0 (facebook, github, google, linkedin). OAuth credentials are automatically cached within a project.

Example Use


req.DevMode() //  Use Client.DevMode to enable debugging details

// There are 2 ways to use req (like many other http clients)
// First way is to create a persistent session client:
client := req.C(). // defaults like timeout and headers can be set for the whole session
    SetUserAgent("my-custom-client").
    SetTimeout(5 * time.Second)
// defaults can be overriden and extended in each request
resp, err := client.R(). // Use R() to create a request and set with chainable request settings.
    SetHeader("Accept", "application/vnd.github.v3+json").
    SetPathParam("username", "imroc").
    SetQueryParam("page", "1").
    SetResult(&result). // Unmarshal response into struct automatically if status code >= 200 and <= 299.
    SetError(&errMsg). // Unmarshal response into struct automatically if status code >= 400.
    EnableDump(). // Enable dump at request level to help troubleshoot, log content only when an unexpected exception occurs.
    Get("https://api.github.com/users/{username}/repos")

// Alternatively, it can be used as is without establishing a client

resp := client.Get("https://api.github.com/users/{username}/repos"). // Create a GET request with specified URL.
    SetHeader("Accept", "application/vnd.github.v3+json").
    SetPathParam("username", "imroc").
    SetQueryParam("page", "1").
    SetResult(&result).
    SetError(&errMsg).
    EnableDump().
    Do() // Send request with Do.
library(httr)

# GET requests:
resp <- GET("http://httpbin.org/get")
status_code(resp)  # status code
headers(resp)  # headers
str(content(resp))  # body

# POST requests: 
# Form encoded
resp <- POST(url, body = body, encode = "form")
# Multipart encoded
resp <- POST(url, body = body, encode = "multipart")
# JSON encoded
resp <- POST(url, body = body, encode = "json")

# setting cookies:
resp <- GET("http://httpbin.org/cookies", set_cookies("MeWant" = "cookies"))
content(r)$cookies  # get response cookies

Alternatives / Similar


Was this page helpful?