Documentation

requests.do() function

The requests.do() function is experimental and subject to change at any time. By using this function, you accept the risks of experimental functions.

requests.do() makes an HTTP request using the specified request method.

import "experimental/http/requests"

requests.do(
    method: "GET",
    url: "http://example.com",
    params: ["example-param": ["example-param-value"]],
    headers: ["Example-Header": "example-header-value"],
    body: bytes(v: ""),
    config: requests.defaultConfig,
)

requests.do() returns a record with the following properties:

  • statusCode: HTTP status code of the request (as an integer).
  • body: Response body (as bytes). A maximum size of 100MB is read from the response body.
  • headers: Response headers (as a dictionary).
  • duration: Request duration (as a duration).

Parameters

method

HTTP request method.

Supported methods:

  • DELETE
  • GET
  • HEAD
  • PATCH
  • POST
  • PUT

url

URL to send the request to.

The URL should not include any query parameters. Use params to specify query parameters.

params

Set of key-value pairs to add to the URL as query parameters. Query parameters are URL-encoded. All values for a key are appended to the query.

headers

Set of key values pairs to include as request headers.

body

Data to send with the request.

config

Set of request configuration options. See HTTP configuration option examples.

Examples

Make a GET request

import "experimental/http/requests"

requests.do(url:"http://example.com", method: "GET")

Make a GET request with authorization

import "experimental/http/requests"
import "influxdata/influxdb/secrets"

token = secrets.get(key:"TOKEN")

requests.do(
    method: "GET",
    url: "http://example.com",
    headers: ["Authorization": "Token ${token}"],
)

Make a GET request with query parameters

import "experimental/http/requests"

requests.do(method: "GET", url: "http://example.com", params: ["start": ["100"]])

Make a GET request and decode the JSON response

To decode a JSON response, import the experimental/json package and use json.parse() to parse the response into a Flux type.

import "experimental/http/requests"
import "experimental/json"
import "array"

response = requests.do(method: "GET", url: "https://api.agify.io", params: ["name": ["nathaniel"]])

// api.agify.io returns JSON with the form
//
// {
//    name: string,
//    age: number,
//    count: number,
// }
//
// Define a data variable that parses the JSON response body into a Flux record.
data = json.parse(data: response.body)

// Use array.from() to construct a table with one row containing our response data.
array.from(rows: [{name: data.name, age: data.age, count: data.count}])

Make a POST request with a JSON body

Use json.encode() to encode a Flux record as a JSON object.

import "experimental/http/requests"
import "json"

requests.do(
    method: "POST",
    url: "https://goolnk.com/api/v1/shorten",
    body: json.encode(v: {url: "http://www.influxdata.com"}),
    headers: ["Content-Type": "application/json"],
)

Output HTTP response data in a table

To quickly inspect HTTP response data, use requests.peek() to output HTTP response data in a table.

import "experimental/http/requests"

response = requests.do(method: "GET", url: "http://example.com")

requests.peek(response: response)

Was this page helpful?

Thank you for your feedback!


Upgrade to InfluxDB Cloud or InfluxDB 2.0!

InfluxDB Cloud and InfluxDB OSS 2.0 ready for production.