Home Explore Blog CI



nushell

3rd chunk of `cookbook/http.md`
39ab524856663cc75cd8b701e833d2c7c5d6495f682fa5090000000100001361
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 }
# => ━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# =>  # │ <value>
# => ───┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
# =>  0 │ {
# =>    │   "userId": 1,
# =>    │   "id": 1,
# =>    │   "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
# =>    │   "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum
# =>    │ rerum est autem sunt rem eveniet architecto"
# =>    │ }
# =>  1 │ {
# =>    │   "userId": 1,
# =>    │   "id": 2,
# =>    │   "title": "qui est esse",
# =>    │   "body": "est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro
# =>    │ vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla"
# =>    │ }
# =>  2 │ {
# =>    │   "userId": 1,
# =>    │   "id": 3,
# =>    │   "title": "ea molestias quasi exercitationem repellat qui ipsa sit aut",
# =>    │   "body": "et iusto sed quo iure\nvoluptatem occaecati omnis eligendi aut ad\nvoluptatem doloribus vel accusantium quis
# =>    │ pariatur\nmolestiae porro eius odio et labore et velit aut"
# =>    │ }
# => ━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
```

---

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
```

Output

```json
[
  {
    "userId": 1,
    "id": 1,
    "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
    "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
  },
  {
    "userId": 1,
    "id": 2,
    "title": "qui est esse",
    "body": "est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla"
  },
  {
    "userId": 1,
    "id": 3,
    "title": "ea molestias quasi exercitationem repellat qui ipsa sit aut",
    "body": "et iusto sed quo iure\nvoluptatem occaecati omnis eligendi aut ad\nvoluptatem doloribus vel accusantium quis pariatur\nmolestiae porro eius odio et labore et velit aut"
  }
]
```

---

Making a `post` request to an endpoint with a JSON payload. To make long requests easier, you can organize your json payloads inside a file.

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

```nu
open payload.json | get my_payload | to json | http post https://jsonplaceholder.typicode.com/posts $in
# => ━━━━━
# =>  id
# => ─────
# =>  101
# => ━━━━━
```

---

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
# => ━━━━━
# =>  id
# => ─────
# =>  101
# => ━━━━━
```

### Uploading files

To upload a form with a file (think a common file upload form in a browser, where you have to select a file and provide some additional data), you need to:

1. Specify the content type as `multipart/form-data`
2. Provide the record as the POST body
3. Provide the file data in one of the record fields as *binary* data.

Title: Combining Responses into a JSON Array, Making POST Requests, and Uploading Files in Nushell
Summary
This section demonstrates how to combine individual JSON responses into a single JSON array using the `to json` command. It also covers making POST requests with JSON payloads, including reading payloads from files and manipulating data before sending it back to an API. Furthermore, it explains how to upload files using the `multipart/form-data` content type, providing the file data as binary within the POST body.