Skip to content

Commit b4eee60

Browse files
authored
Flatten the JSON files in /cookbook/http.md (#2127)
1 parent 83f96bd commit b4eee60

1 file changed

Lines changed: 13 additions & 17 deletions

File tree

cookbook/http.md

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,15 @@ perhaps with different query parameters and you want to view all the responses a
4747
An example JSON file, `urls.json`, with the following contents:
4848

4949
```json
50-
{
51-
"urls": [
52-
"https://jsonplaceholder.typicode.com/posts/1",
53-
"https://jsonplaceholder.typicode.com/posts/2",
54-
"https://jsonplaceholder.typicode.com/posts/3"
55-
]
56-
}
50+
[
51+
"https://jsonplaceholder.typicode.com/posts/1",
52+
"https://jsonplaceholder.typicode.com/posts/2",
53+
"https://jsonplaceholder.typicode.com/posts/3"
54+
]
5755
```
5856

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

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

118116
```nu
119-
open urls.json | get urls | each { |u| http get $u } | to json
117+
open urls.json | each { |u| http get $u } | to json
120118
```
121119

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

151149
```json
152150
{
153-
"my_payload": {
154-
"title": "foo",
155-
"body": "bar",
156-
"userId": 1
157-
}
151+
"title": "foo",
152+
"body": "bar",
153+
"userId": 1
158154
}
159155
```
160156

161157
```nu
162-
open payload.json | get my_payload | to json | http post https://jsonplaceholder.typicode.com/posts $in
158+
open payload.json | to json | http post https://jsonplaceholder.typicode.com/posts $in
163159
# => ━━━━━
164160
# => id
165161
# => ─────
@@ -172,7 +168,7 @@ open payload.json | get my_payload | to json | http post https://jsonplaceholder
172168
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.
173169

174170
```nu
175-
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
171+
open urls.json | first | http get $in | upsert id {|item| $item.id | inc} | to json | http post https://jsonplaceholder.typicode.com/posts $in
176172
# => ━━━━━
177173
# => id
178174
# => ─────

0 commit comments

Comments
 (0)