From 2e151cf628a5e0001659534baf908d7bacc113bd Mon Sep 17 00:00:00 2001 From: Scott Anderson Date: Thu, 3 Feb 2022 17:02:51 -0700 Subject: [PATCH] hotfix: add more examples to requests package functions --- .../stdlib/experimental/http/requests/do.md | 21 ++++++++++- .../stdlib/experimental/http/requests/get.md | 30 +++++++++++++++ .../stdlib/experimental/http/requests/post.md | 37 ++++++++++++++++++- 3 files changed, 86 insertions(+), 2 deletions(-) diff --git a/content/flux/v0.x/stdlib/experimental/http/requests/do.md b/content/flux/v0.x/stdlib/experimental/http/requests/do.md index 9d4424c68..ebbc07b76 100644 --- a/content/flux/v0.x/stdlib/experimental/http/requests/do.md +++ b/content/flux/v0.x/stdlib/experimental/http/requests/do.md @@ -66,6 +66,7 @@ Data to send with the request. ### config {data-type="record"} Set of request configuration options. +_See [HTTP configuration option examples](/flux/v0.x/stdlib/experimental/http/requests/#examples)._ ## Examples @@ -98,10 +99,28 @@ requests.do( ```js import "experimental/http/requests" -resp = requests.do( +requests.do( method: "GET", url: "http://example.com", params: ["start": ["100"]], ) ``` +### Output HTTP response data in a table +```js +import "array" +import "dict" +import "experimental/http/requests" + +resp = requests.do(method: "GET", url: "http://example.com") + +array.from( + rows: [ + { + body: string(v: resp.body), + statusCode: resp.statusCode, + date: dict.get(dict: resp.headers, key: "Date", default: ""), + }, + ], +) +``` \ No newline at end of file diff --git a/content/flux/v0.x/stdlib/experimental/http/requests/get.md b/content/flux/v0.x/stdlib/experimental/http/requests/get.md index 9bb1ae86e..534f5fc80 100644 --- a/content/flux/v0.x/stdlib/experimental/http/requests/get.md +++ b/content/flux/v0.x/stdlib/experimental/http/requests/get.md @@ -54,6 +54,7 @@ Data to send with the request. ### config {data-type="record"} Set of request configuration options. +_See [HTTP configuration option examples](/flux/v0.x/stdlib/experimental/http/requests/#examples)._ ## Examples @@ -63,3 +64,32 @@ import "experimental/http/requests" requests.get(url:"http://example.com") ``` + +### Make a GET request with authorization +```js +import "experimental/http/requests" +import "influxdata/influxdb/secrets" + +token = secrets.get(key: "TOKEN") + +requests.get(url: "http://example.com", headers: ["Authorization": "Bearer ${token}"]) +``` + +### Output HTTP response data in a table +```js +import "array" +import "dict" +import "experimental/http/requests" + +resp = requests.get(url: "http://example.com") + +array.from( + rows: [ + { + body: string(v: resp.body), + statusCode: resp.statusCode, + date: dict.get(dict: resp.headers, key: "Date", default: ""), + }, + ], +) +``` diff --git a/content/flux/v0.x/stdlib/experimental/http/requests/post.md b/content/flux/v0.x/stdlib/experimental/http/requests/post.md index 1049a5318..0e9756e4c 100644 --- a/content/flux/v0.x/stdlib/experimental/http/requests/post.md +++ b/content/flux/v0.x/stdlib/experimental/http/requests/post.md @@ -54,6 +54,7 @@ Data to send with the request. ### config {data-type="record"} Set of request configuration options. +_See [HTTP configuration option examples](/flux/v0.x/stdlib/experimental/http/requests/#examples)._ ## Examples @@ -62,5 +63,39 @@ Set of request configuration options. import "json" import "experimental/http/requests" -resp = requests.post(url:"http://example.com", body: json.encode(v: {data: {x:1, y: 2, z:3})) +requests.post(url:"http://example.com", body: json.encode(v: {data: {x:1, y: 2, z:3})) +``` + +### Make a POST request with authorization +```js +import "json" +import "experimental/http/requests" +import "influxdata/influxdb/secrets" + +token = secrets.get(key: "TOKEN") + +requests.post( + url: "http://example.com", + body: json.encode(v: {data: {x: 1, y: 2, z: 3}}), + headers: ["Authorization": "Bearer ${token}"], +) +``` + +### Output HTTP response data in a table +```js +import "array" +import "dict" +import "experimental/http/requests" + +resp = requests.post(url: "http://example.com") + +array.from( + rows: [ + { + body: string(v: resp.body), + statusCode: resp.statusCode, + date: dict.get(dict: resp.headers, key: "Date", default: ""), + }, + ], +) ```