send_api_request: Send API Request to a Specified URL

View source: R/send_api_request.R

send_api_requestR Documentation

Send API Request to a Specified URL

Description

This function sends an HTTP request (GET or POST) to the specified URL. It supports optional request bodies for POST requests, customizable encoding, and content type for API interactions. The function is designed to be a general-purpose API handler for use in querying external APIs.

Usage

send_api_request(
  url,
  method = "GET",
  body = NULL,
  encode = "json",
  content_type = "application/json",
  verbosity = TRUE
)

Arguments

url

A string representing the target URL for the API request.

method

A string specifying the HTTP method to use. The default is "GET", but "POST" can also be used.

body

Optional: The body of the request, typically required for POST requests. Default is NULL.

encode

A string representing the encoding type of the body for POST requests. Default is "json".

content_type

A string specifying the content type for POST requests. Default is "application/json".

verbosity

Logical flag indicating whether to print status messages during the function execution. Default is TRUE.

Details

The send_api_request function is a flexible tool for handling API interactions. It supports both GET and POST methods and provides optional parameters for encoding and content type, making it suitable for a wide range of API requests.

If a network error occurs during the request, the function will throw an error with a detailed message about the failure.

Value

A response object from the httr package representing the server's response to the API request.


rPDBapi documentation built on Sept. 11, 2024, 6:37 p.m.