diff --git a/content/flux/v0.x/data-types/basic/bytes.md b/content/flux/v0.x/data-types/basic/bytes.md index e77eb6c5a..13a91949f 100644 --- a/content/flux/v0.x/data-types/basic/bytes.md +++ b/content/flux/v0.x/data-types/basic/bytes.md @@ -21,6 +21,7 @@ A **bytes** type represents a sequence of byte values. - [Bytes syntax](#bytes-syntax) - [Convert a column to bytes](#convert-a-column-to-bytes) +- [Include the string representation of bytes in a table](#include-the-string-representation-of-bytes-in-a-table) ## Bytes syntax Flux does not provide a bytes literal syntax. @@ -49,3 +50,37 @@ import "contrib/bonitoo-io/hex" hex.bytes(v: "FF5733") // Returns [255 87 51] (bytes) ``` + +## Include the string representation of bytes in a table + +Use [`display()`](/flux/v0.x/stdlib/universe/display/) to return the string +representation of bytes and include it as a column value. +`display()` represents bytes types as a string of lowercase hexadecimal +characters prefixed with `0x`. + +```js +import "sampledata" + +sampledata.string() + |> map(fn: (r) => ({r with _value: display(v: bytes(v: r._value))})) +``` + +#### Output + +| tag | _time | _value (string) | +| --- | :------------------- | ------------------------------------------: | +| t1 | 2021-01-01T00:00:00Z | 0x736d706c5f673971637a73 | +| t1 | 2021-01-01T00:00:10Z | 0x736d706c5f306d6776396e | +| t1 | 2021-01-01T00:00:20Z | 0x736d706c5f706877363634 | +| t1 | 2021-01-01T00:00:30Z | 0x736d706c5f6775767a7934 | +| t1 | 2021-01-01T00:00:40Z | 0x736d706c5f357633636365 | +| t1 | 2021-01-01T00:00:50Z | 0x736d706c5f7339666d6779 | + +| tag | _time | _value (string) | +| --- | :------------------- | ------------------------------------------: | +| t2 | 2021-01-01T00:00:00Z | 0x736d706c5f623565696461 | +| t2 | 2021-01-01T00:00:10Z | 0x736d706c5f6575346f7870 | +| t2 | 2021-01-01T00:00:20Z | 0x736d706c5f356737747a34 | +| t2 | 2021-01-01T00:00:30Z | 0x736d706c5f736f78317574 | +| t2 | 2021-01-01T00:00:40Z | 0x736d706c5f77666d373537 | +| t2 | 2021-01-01T00:00:50Z | 0x736d706c5f64746e326276 | diff --git a/content/flux/v0.x/data-types/composite/array.md b/content/flux/v0.x/data-types/composite/array.md index adb5a1453..b4d4653d3 100644 --- a/content/flux/v0.x/data-types/composite/array.md +++ b/content/flux/v0.x/data-types/composite/array.md @@ -66,6 +66,8 @@ arr[2] - [Get the length of an array](#get-the-length-of-an-array) - [Create a stream of tables from an array](#create-a-stream-of-tables-from-an-array) - [Compare arrays](#compare-arrays) +- [Return the string representation of an array](#return-the-string-representation-of-an-array) +- [Include the string representation of an array in a table](#include-the-string-representation-of-an-array-in-a-table) ### Iterate over an array {{% note %}} @@ -120,7 +122,6 @@ array.from(rows: arr) | Jane | Doe | 32 | | Jack | Smith | 56 | - ### Compare arrays Use the `==` [comparison operator](/flux/v0.x/spec/operators/#comparison-operators) to check if two arrays are equal. @@ -132,4 +133,43 @@ Equality is based on values, their type, and order. [12300.0, 34500.0] == [float(v: "1.23e+04"), float(v: "3.45e+04")] // Returns true -``` \ No newline at end of file +``` + +### Return the string representation of an array +Use [`display()`](/flux/v0.x/stdlib/universe/display/) to return Flux literal +representation of an array as a string. + +```js +arr = [1, 2, 3] + +display(v: arr) + +// Returns "[1, 2, 3]" +``` + +### Include the string representation of an array in a table +Use [`display()`](/flux/v0.x/stdlib/universe/display/) to return Flux literal +representation of an array as a string and include it as a column value. + +```js +import "sampledata" + +sampledata.string() + |> map(fn: (r) => ({_time: r._time, exampleArray: display(v: [r.tag, r._value])})) +``` + +#### Output +| _time (time) | exampleArray (string) | +| :--------------------------------------- | :----------------------------------------------- | +| 2021-01-01T00:00:00Z | [t1, smpl_g9qczs] | +| 2021-01-01T00:00:10Z | [t1, smpl_0mgv9n] | +| 2021-01-01T00:00:20Z | [t1, smpl_phw664] | +| 2021-01-01T00:00:30Z | [t1, smpl_guvzy4] | +| 2021-01-01T00:00:40Z | [t1, smpl_5v3cce] | +| 2021-01-01T00:00:50Z | [t1, smpl_s9fmgy] | +| 2021-01-01T00:00:00Z | [t2, smpl_b5eida] | +| 2021-01-01T00:00:10Z | [t2, smpl_eu4oxp] | +| 2021-01-01T00:00:20Z | [t2, smpl_5g7tz4] | +| 2021-01-01T00:00:30Z | [t2, smpl_sox1ut] | +| 2021-01-01T00:00:40Z | [t2, smpl_wfm757] | +| 2021-01-01T00:00:50Z | [t2, smpl_dtn2bv] | diff --git a/content/flux/v0.x/data-types/composite/dict.md b/content/flux/v0.x/data-types/composite/dict.md index 28269d63e..e7b53f8cb 100644 --- a/content/flux/v0.x/data-types/composite/dict.md +++ b/content/flux/v0.x/data-types/composite/dict.md @@ -80,6 +80,8 @@ dict.get(dict: positions, key: "Teller", default: "Unknown position") - [Create a dictionary from a list](#create-a-dictionary-from-a-list) - [Insert a key-value pair into a dictionary](#insert-a-key-value-pair-into-a-dictionary) - [Remove a key-value pair from a dictionary](#remove-a-key-value-pair-from-a-dictionary) +- [Return the string representation of a dictionary](#return-the-string-representation-of-a-dictionary) +- [Include the string representation of a dictionary in a table](#include-the-string-representation-of-a-dictionary-in-a-table) ### Create a dictionary from a list 1. Import the [`dict` package](/flux/v0.x/stdlib/dict/). @@ -133,3 +135,43 @@ dict.remove( ) // Returns [k1: v1] ``` + +### Return the string representation of a dictionary +Use [`display()`](/flux/v0.x/stdlib/universe/display/) to return Flux literal +representation of a dictionary as a string. + +```js +x = ["a": 1, "b": 2, "c": 3] + +display(v: x) + +// Returns "[a: 1, b: 2, c: 3]" +``` + +### Include the string representation of a dictionary in a table +Use [`display()`](/flux/v0.x/stdlib/universe/display/) to return Flux literal +representation of a dictionary as a string and include it as a column value. + +```js +import "sampledata" + +sampledata.string() + |> map(fn: (r) => ({_time: r._time, exampleDict: display(v: ["tag": r.tag, "value":r._value])})) +``` + +#### Output + +| \_time (time) | exampleDict (string) | +| :---------------------------------------- | :----------------------------------------------- | +| 2021-01-01T00:00:00Z | [tag: t1, value: smpl_g9qczs] | +| 2021-01-01T00:00:10Z | [tag: t1, value: smpl_0mgv9n] | +| 2021-01-01T00:00:20Z | [tag: t1, value: smpl_phw664] | +| 2021-01-01T00:00:30Z | [tag: t1, value: smpl_guvzy4] | +| 2021-01-01T00:00:40Z | [tag: t1, value: smpl_5v3cce] | +| 2021-01-01T00:00:50Z | [tag: t1, value: smpl_s9fmgy] | +| 2021-01-01T00:00:00Z | [tag: t2, value: smpl_b5eida] | +| 2021-01-01T00:00:10Z | [tag: t2, value: smpl_eu4oxp] | +| 2021-01-01T00:00:20Z | [tag: t2, value: smpl_5g7tz4] | +| 2021-01-01T00:00:30Z | [tag: t2, value: smpl_sox1ut] | +| 2021-01-01T00:00:40Z | [tag: t2, value: smpl_wfm757] | +| 2021-01-01T00:00:50Z | [tag: t2, value: smpl_dtn2bv] | diff --git a/content/flux/v0.x/data-types/composite/function.md b/content/flux/v0.x/data-types/composite/function.md index c06ade7c9..171075af2 100644 --- a/content/flux/v0.x/data-types/composite/function.md +++ b/content/flux/v0.x/data-types/composite/function.md @@ -63,6 +63,3 @@ A Flux **function** literal contains the following: ## Define functions _For information about defining custom functions, see [Define custom functions](/flux/v0.x/define-functions/)._ - - - diff --git a/content/flux/v0.x/data-types/composite/record.md b/content/flux/v0.x/data-types/composite/record.md index 3b7767533..a487c388e 100644 --- a/content/flux/v0.x/data-types/composite/record.md +++ b/content/flux/v0.x/data-types/composite/record.md @@ -136,6 +136,8 @@ _To dynamically reference keys in a composite type, consider using a - [Extend a record](#extend-a-record) - [List keys in a record](#list-keys-in-a-record) - [Compare records](#compare-records) +- [Return the string representation of a record](#return-the-string-representation-of-a-record) +- [Include the string representation of a record in a table](#include-the-string-representation-of-a-record-in-a-table) ### Extend a record Use the **`with` operator** to extend a record. @@ -181,3 +183,43 @@ Equality is based on keys, their values, and types. {foo: 12300.0, bar: 34500.0} == {bar: float(v: "3.45e+04"), foo: float(v: "1.23e+04")} // Returns true ``` + +### Return the string representation of a record +Use [`display()`](/flux/v0.x/stdlib/universe/display/) to return the Flux literal +representation of a record as a string. + +```js +x = {a: 1, b: 2, c: 3} + +display(v: x) + +// Returns "{a: 1, b: 2, c: 3}" +``` + +### Include the string representation of a record in a table +Use [`display()`](/flux/v0.x/stdlib/universe/display/) to return the Flux literal +representation of a record as a string and include it as a column value. + +```js +import "sampledata" + +sampledata.string() + |> map(fn: (r) => ({_time: r._time, exampleRecord: display(v: {tag: r.tag, value:r._value})})) +``` + +#### Output + +| \_time (time) | exampleRecord (string) | +| :---------------------------------------- | :----------------------------------------------- | +| 2021-01-01T00:00:00Z | {tag: t1, value: smpl_g9qczs} | +| 2021-01-01T00:00:10Z | {tag: t1, value: smpl_0mgv9n} | +| 2021-01-01T00:00:20Z | {tag: t1, value: smpl_phw664} | +| 2021-01-01T00:00:30Z | {tag: t1, value: smpl_guvzy4} | +| 2021-01-01T00:00:40Z | {tag: t1, value: smpl_5v3cce} | +| 2021-01-01T00:00:50Z | {tag: t1, value: smpl_s9fmgy} | +| 2021-01-01T00:00:00Z | {tag: t2, value: smpl_b5eida} | +| 2021-01-01T00:00:10Z | {tag: t2, value: smpl_eu4oxp} | +| 2021-01-01T00:00:20Z | {tag: t2, value: smpl_5g7tz4} | +| 2021-01-01T00:00:30Z | {tag: t2, value: smpl_sox1ut} | +| 2021-01-01T00:00:40Z | {tag: t2, value: smpl_wfm757} | +| 2021-01-01T00:00:50Z | {tag: t2, value: smpl_dtn2bv} | diff --git a/content/flux/v0.x/get-started/syntax-basics.md b/content/flux/v0.x/get-started/syntax-basics.md index e3e5c595c..d12376405 100644 --- a/content/flux/v0.x/get-started/syntax-basics.md +++ b/content/flux/v0.x/get-started/syntax-basics.md @@ -28,6 +28,7 @@ This guide walks through how Flux handles a few simple expressions. - [Dictionaries](#dictionaries) - [Functions](#functions) - [Regular expression types](#regular-expression-types) + - [View the string representation of any Flux type](#view-the-string-representation-of-any-flux-type) - [Packages](#packages) - [Examples of basic syntax](#examples-of-basic-syntax) - [Define data stream variables](#define-data-stream-variables) @@ -149,6 +150,7 @@ The following basic types do not have a literal syntax, but can be created in ot ### Composite Types Flux [composite types](/flux/v0.x/data-types/composite/) are constructed from Flux [basic types](#basic-types). +All composite types have a Flux literal representation. - [Records](#records) - [Arrays](#arrays) @@ -281,6 +283,17 @@ regex = /^foo/ // Returns false ``` +### View the string representation of any Flux type +Use [`display()`](/flux/v0.x/stdlib/universe/display) to output the Flux literal +representation of any value as a string. + +```js +x = bytes(v: "foo") + +display(v: x) +// Returns "0x666f6f" +``` + ## Packages The [Flux standard library](/flux/v0.x/stdlib/) is organized into [packages](/flux/v0.x/spec/packages/) that contain functions and package-specific options. diff --git a/content/flux/v0.x/release-notes.md b/content/flux/v0.x/release-notes.md index a2aabb32c..fb16df1eb 100644 --- a/content/flux/v0.x/release-notes.md +++ b/content/flux/v0.x/release-notes.md @@ -10,6 +10,33 @@ aliases: - /influxdb/cloud/reference/release-notes/flux/ --- +## v0.154.0 [2022-02-09] + +### Features +- Add [`requests.peek()`](/flux/v0.x/stdlib/experimental/http/requests/peek/) to + return HTTP response data in a table. +- Add [`display()`](/flux/v0.x/stdlib/universe/display/) to represent any value as a string. +- Create a version of `map()` that is columnar and supports vectorization. +- Support vectorized functions. + +### Bug fixes +- Add time vector to the `values` package. +- Set the correct type for vectorized functions. + +--- + +## v0.153.0 [2022-02-07] + +### Features +- Connect language server protocol (LSP) features through the Flux crate. +- Add conversion from `flux.Bounds` to `plan/execute.Bounds`. +- Re-index all bound variables to start from 0. + +### Bug fixes +- Int feature flags work properly when returned as floats. + +--- + ## v0.152.0 [2022-01-31] ### Features diff --git a/content/flux/v0.x/stdlib/experimental/http/requests/_index.md b/content/flux/v0.x/stdlib/experimental/http/requests/_index.md index 022feb36e..cf8965d70 100644 --- a/content/flux/v0.x/stdlib/experimental/http/requests/_index.md +++ b/content/flux/v0.x/stdlib/experimental/http/requests/_index.md @@ -85,6 +85,7 @@ config = {defaultConfig with // extending the default. timeout: 60s, } +response = requests.get(url:"http://example.com", config: config) -requests.get(url:"http://example.com", config: config) +requests.peek(response: response) ``` \ No newline at end of file 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 ebbc07b76..e0dd573b1 100644 --- a/content/flux/v0.x/stdlib/experimental/http/requests/do.md +++ b/content/flux/v0.x/stdlib/experimental/http/requests/do.md @@ -7,7 +7,7 @@ menu: name: requests.do parent: requests weight: 401 -flux/v0.x/tags: [http, inputs] +flux/v0.x/tags: [http, inputs, outputs] introduced: 0.152.0 --- @@ -19,8 +19,8 @@ import "experimental/http/requests" requests.do( method: "GET", url: "http://example.com", - params: [:], - headers: [:], + params: ["example-param": ["example-param-value"]], + headers: ["Example-Header": "example-header-value"], body: bytes(v: ""), config: requests.defaultConfig, ) @@ -28,9 +28,11 @@ requests.do( `requests.do()` returns a record with the following properties: -- **statusCode**: HTTP status code of the request. -- **body**: Response body. A maximum size of 100MB is read from the response body. -- **headers**: Response headers. +- **statusCode**: HTTP status code of the request _(as an [integer](/flux/v0.x/data-types/basic/int/))_. +- **body**: Response body _(as [bytes](/flux/v0.x/data-types/basic/bytes/))_. + A maximum size of 100MB is read from the response body. +- **headers**: Response headers _(as a [dictionary](/flux/v0.x/data-types/composite/dict/))_. +- **duration**: Request duration _(as a [duration](/flux/v0.x/data-types/basic/duration/))_. ## Parameters @@ -73,6 +75,9 @@ _See [HTTP configuration option examples](/flux/v0.x/stdlib/experimental/http/re - [Make a GET request](#make-a-get-request) - [Make a GET request with authorization](#make-a-get-request-with-authorization) - [Make a GET request with query parameters](#make-a-get-request-with-query-parameters) +- [Make a GET request and decode the JSON response](#make-a-get-request-and-decode-the-json-response) +- [Make a POST request with a JSON body](#make-a-post-request-with-a-json-body) +- [Output HTTP response data in a table](#output-http-response-data-in-a-table) ### Make a GET request ```js @@ -106,21 +111,57 @@ requests.do( ) ``` -### Output HTTP response data in a table +### Make a GET request and decode the JSON response +To decode a JSON response, import the [`experimental/json` package](/flux/v0.x/stdlib/experimental/json/) +and use [`json.parse()`](/flux/v0.x/stdlib/experimental/json/parse/) to parse +the response into a [Flux type](/flux/v0.x/data-types/). + ```js +import "experimental/http/requests" +import "experimental/json" import "array" -import "dict" + +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()`](/flux/v0.x/stdlib/json/encode/) to encode a Flux record as +a JSON object. + +```js +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()`](/flux/v0.x/stdlib/experimental/http/requests/peek/) +to output HTTP response data in a table. + +```js import "experimental/http/requests" -resp = requests.do(method: "GET", url: "http://example.com") +response = 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: ""), - }, - ], -) +requests.peek(response: response) ``` \ 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 534f5fc80..b95d8c667 100644 --- a/content/flux/v0.x/stdlib/experimental/http/requests/get.md +++ b/content/flux/v0.x/stdlib/experimental/http/requests/get.md @@ -7,7 +7,7 @@ menu: name: requests.get parent: requests weight: 401 -flux/v0.x/tags: [http, inputs] +flux/v0.x/tags: [http, inputs, outputs] introduced: 0.152.0 --- @@ -18,8 +18,8 @@ import "experimental/http/requests" requests.get( url: "http://example.com", - params: [:], - headers: [:], + params: ["example-param": ["example-param-value"]], + headers: ["Example-Header": "example-header-value"], body: bytes(v: ""), config: requests.defaultConfig, ) @@ -58,6 +58,11 @@ _See [HTTP configuration option examples](/flux/v0.x/stdlib/experimental/http/re ## Examples +- [Make a GET request](#make-a-get-request) +- [Make a GET request with authorization](#make-a-get-request-with-authorization) +- [Make a GET request and decode the JSON response](#make-a-get-request-and-decode-the-json-response) +- [Output HTTP response data in a table](#output-http-response-data-in-a-table) + ### Make a GET request ```js import "experimental/http/requests" @@ -75,21 +80,41 @@ token = secrets.get(key: "TOKEN") requests.get(url: "http://example.com", headers: ["Authorization": "Bearer ${token}"]) ``` -### Output HTTP response data in a table +### Make a GET request and decode the JSON response +To decode a JSON response, import the [`experimental/json` package](/flux/v0.x/stdlib/experimental/json/) +and use [`json.parse()`](/flux/v0.x/stdlib/experimental/json/parse/) to parse +the response into a [Flux type](/flux/v0.x/data-types/). + ```js +import "experimental/http/requests" +import "experimental/json" import "array" -import "dict" + +response = requests.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}]) +``` + +### Output HTTP response data in a table +To quickly inspect HTTP response data, use [`requests.peek()`](/flux/v0.x/stdlib/experimental/http/requests/peek/) +to output HTTP response data in a table. + +```js import "experimental/http/requests" -resp = requests.get(url: "http://example.com") +response = 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: ""), - }, - ], -) +requests.peek(response: response) ``` diff --git a/content/flux/v0.x/stdlib/experimental/http/requests/peek.md b/content/flux/v0.x/stdlib/experimental/http/requests/peek.md new file mode 100644 index 000000000..950df2b45 --- /dev/null +++ b/content/flux/v0.x/stdlib/experimental/http/requests/peek.md @@ -0,0 +1,74 @@ +--- +title: requests.peek() function +description: > + `requests.peek()` converts an HTTP response into a table for easy inspection. +menu: + flux_0_x_ref: + name: requests.peek + parent: requests +weight: 401 +flux/v0.x/tags: [http] +introduced: 0.154.0 +--- + +`requests.peek()` converts an HTTP response into a table for easy inspection. + +```js +import "experimental/http/requests" + +requests.peek( + response: requests.get(url: "http://example.com") +) +``` + +The output table includes the following columns: + +- **body**: response body as a string +- **statusCode**: returned status code as an integer +- **headers**: string representation of response headers +- **duration**: request duration in nanoseconds + +{{% note %}} +To customize how the response data is structured in a table, use `array.from()` +with a function like `json.parse()`. Parse the response body into a set of values +and then use `array.from()` to construct a table from those values. +{{% /note %}} + + +## Parameters + +### response {data-type="record"} +Response data from an HTTP request. + +## Examples + +### Inspect the response of an HTTP request +```js +import "experimental/http/requests" + +response = requests.get(url: "https://api.agify.io", params: ["name": ["natalie"]]) + +requests.peek(response: response) +``` + +| statusCode | body | headers | duration | +| :--------- | :--- | :------ | -------: | +| 200 | {"name":"natalie","age":34,"count":20959} | _See [returned headers](#returned-headers) string below_ | 1212263875 | + +##### Returned headers +``` +[ + Access-Control-Allow-Headers: Content-Type, X-Genderize-Source, + Access-Control-Allow-Methods: GET, + Access-Control-Allow-Origin: *, + Connection: keep-alive, + Content-Length: 41, + Content-Type: application/json; charset=utf-8, + Date: Wed, 09 Feb 2022 20:00:00 GMT, + Etag: W/"29-klDahUESBLxHyQ7NiaetCn2CvCI", + Server: nginx/1.16.1, + X-Rate-Limit-Limit: 1000, + X-Rate-Limit-Remaining: 999, + X-Rate-Reset: 12203 +] +``` 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 0e9756e4c..c76fdf3e6 100644 --- a/content/flux/v0.x/stdlib/experimental/http/requests/post.md +++ b/content/flux/v0.x/stdlib/experimental/http/requests/post.md @@ -7,7 +7,7 @@ menu: name: requests.post parent: requests weight: 401 -flux/v0.x/tags: [http, inputs] +flux/v0.x/tags: [http, inputs, outputs] introduced: 0.152.0 --- @@ -18,8 +18,8 @@ import "experimental/http/requests" requests.post( url: "http://example.com", - params: [:], - headers: [:], + params: ["example-param": ["example-param-value"]], + headers: ["Example-Header": "example-header-value"], body: bytes(v: ""), config: requests.defaultConfig, ) @@ -58,6 +58,11 @@ _See [HTTP configuration option examples](/flux/v0.x/stdlib/experimental/http/re ## Examples +- [Make a POST request](#make-a-post-request) +- [Make a POST request with authorization](#make-a-post-request-with-authorization) +- [Make a POST request with a JSON body](#make-a-post-request-with-a-json-body) +- [Output HTTP POST response data in a table](#output-http-post-response-data-in-a-table) + ### Make a POST request ```js import "json" @@ -81,21 +86,29 @@ requests.post( ) ``` -### Output HTTP response data in a table +### Make a POST request with a JSON body +Use [`json.encode()`](/flux/v0.x/stdlib/json/encode/) to encode a Flux record as +a JSON object. + ```js -import "array" -import "dict" import "experimental/http/requests" +import "json" -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: ""), - }, - ], +requests.post( + url: "https://goolnk.com/api/v1/shorten", + body: json.encode(v: {url: "http://www.influxdata.com"}), + headers: ["Content-Type": "application/json"], ) ``` + +### Output HTTP POST response data in a table +To quickly inspect HTTP response data, use [`requests.peek()`](/flux/v0.x/stdlib/experimental/http/requests/peek/) +to output HTTP response data in a table. + +```js +import "experimental/http/requests" + +response = requests.post(url: "http://example.com") + +requests.peek(repsonse: response) +``` diff --git a/content/flux/v0.x/stdlib/universe/display.md b/content/flux/v0.x/stdlib/universe/display.md new file mode 100644 index 000000000..c679cdbf4 --- /dev/null +++ b/content/flux/v0.x/stdlib/universe/display.md @@ -0,0 +1,124 @@ +--- +title: display() function +description: > + `display()` returns the Flux literal representation of any value as a string. +menu: + flux_0_x_ref: + name: display + parent: universe +weight: 102 +introduced: 0.154.0 +--- + +`display()` returns the Flux literal representation of any value as a string. + +```js +display(v: "example value") +``` + +[Basic types](/flux/v0.x/data-types/basic/) are converted directly to a string. +[Bytes types](/flux/v0.x/data-types/basic/bytes/) are represented as a string of +lowercase hexadecimal characters prefixed with `0x`. +[Composite types](/flux/v0.x/data-types/composite/) (arrays, dictionaries, and records) +are represented in a syntax similar to their equivalent Flux literal representation. + +Note the following about the resulting string representation: + +- It cannot always be parsed back into the original value. +- It may span multiple lines. +- It may change between Flux versions. + +{{% note %}} +`display()` differs from [`string()`](/flux/v0.x/stdlib/universe/string/) in +that `display()` recursively converts values inside composite types to strings. +`string()` does not operate on composite types. +{{% /note %}} + +## Parameters + +### v +Value to convert for display. + +## Examples + +- [Display composite values as part of a table](#display-composite-values-as-part-of-a-table) +- [Display a record](#display-a-record) +- [Display an array](#display-an-array) +- [Display a dictionary](#display-a-dictionary) +- [Display bytes](#display-bytes) +- [Display a composite value](#display-a-composite-value) + +### Display composite values as part of a table +Use [`array.from()`](/flux/v0.x/stdlib/array/from/) and `display()` to quickly +observe any value. + +```js +import "array" + +array.from(rows:[{ + dict: display(v: ["a":1, "b": 2]), + record: display(v:{x: 1, y: 2}), + array: display(v: [5,6,7]) +}]) +``` + +#### Output data +| dict | record | array | +| :----------- | :----------- | :-------- | +| [a: 1, b: 2] | {x: 1, y: 2} | [5, 6, 7] | + +### Display a record +```js +x = {a: 1, b: 2, c: 3} + +display(v: x) + +// Returns {a: 1, b: 2, c: 3} +``` + +### Display an array +```js +x = [1, 2, 3] + +display(v: x) + +// Returns [1, 2, 3] +``` + +### Display a dictionary +```js +x = ["a": 1, "b": 2, "c": 3] + +display(v: x) + +// Returns [a: 1, b: 2, c: 3] +``` + +### Display bytes +```js +x = bytes(v:"abc") + +display(v: x) + +// Returns 0x616263 +``` + +### Display a composite value +```js +x = { + bytes: bytes(v: "abc"), + string: "str", + array: [1,2,3], + dict: ["a": 1, "b": 2, "c": 3], +} + +display(v: x) + +// Returns +// { +// array: [1, 2, 3], +// bytes: 0x616263, +// dict: [a: 1, b: 2, c: 3], +// string: str +// } +``` \ No newline at end of file