Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 13 additions & 17 deletions cookbook/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,15 @@ perhaps with different query parameters and you want to view all the responses a
An example JSON file, `urls.json`, with the following contents:

```json
{
"urls": [
"https://jsonplaceholder.typicode.com/posts/1",
"https://jsonplaceholder.typicode.com/posts/2",
"https://jsonplaceholder.typicode.com/posts/3"
]
}
[
"https://jsonplaceholder.typicode.com/posts/1",
"https://jsonplaceholder.typicode.com/posts/2",
"https://jsonplaceholder.typicode.com/posts/3"
]
```

```nu
open urls.json | get urls | each { |u| http get $u }
open urls.json | each { |u| http get $u }
# => ━━━┯━━━━━━━━┯━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# => # │ userId │ id │ title │ body
# => ───┼────────┼────┼─────────────────────────────────────────────────────────┼──────────────────────────────────────────────────────────
Expand All @@ -83,7 +81,7 @@ open urls.json | get urls | each { |u| http get $u }
If you specify the `--raw` flag, you'll see 3 separate json objects, one in each row.

```nu
open urls.json | get urls | each { |u| http get $u -r }
open urls.json | each { |u| http get $u -r }
# => ━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# => # │ <value>
# => ───┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Expand Down Expand Up @@ -116,7 +114,7 @@ open urls.json | get urls | each { |u| http get $u -r }
To combine these responses together into a valid JSON array, you can turn the table into json.

```nu
open urls.json | get urls | each { |u| http get $u } | to json
open urls.json | each { |u| http get $u } | to json
```

Output
Expand Down Expand Up @@ -150,16 +148,14 @@ Making a `post` request to an endpoint with a JSON payload. To make long request

```json
{
"my_payload": {
"title": "foo",
"body": "bar",
"userId": 1
}
"title": "foo",
"body": "bar",
"userId": 1
}
```

```nu
open payload.json | get my_payload | to json | http post https://jsonplaceholder.typicode.com/posts $in
open payload.json | to json | http post https://jsonplaceholder.typicode.com/posts $in
# => ━━━━━
# => id
# => ─────
Expand All @@ -172,7 +168,7 @@ open payload.json | get my_payload | to json | http post https://jsonplaceholder
We can put this all together into a pipeline where we read data, manipulate it, and then send it back to the API. Lets `fetch` a post, `increment` the id, and `post` it back to the endpoint. In this particular example, the test endpoint gives back an arbitrary response which we can't actually mutate.

```nu
open urls.json | get urls | first | http get $in | upsert id {|item| $item.id | inc} | to json | http post https://jsonplaceholder.typicode.com/posts $in
open urls.json | first | http get $in | upsert id {|item| $item.id | inc} | to json | http post https://jsonplaceholder.typicode.com/posts $in
# => ━━━━━
# => id
# => ─────
Expand Down