HTTP

Fetching JSON from a url

  1. > fetch https://jsonplaceholder.typicode.com/posts | first 5

Output

  1. ━━━┯━━━━━━━━┯━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  2. # │ userId │ id │ title │ body
  3. ───┼────────┼────┼─────────────────────────────────────────────────────────┼──────────────────────────────────────────────────────────
  4. 0 1 1 sunt aut facere repellat provident occaecati excepturi quia et suscipit
  5. optio reprehenderit suscipit recusandae consequuntur expedita et cum
  6. reprehenderit molestiae ut ut quas totam
  7. nostrum rerum est autem sunt rem eveniet architecto
  8. 1 1 2 qui est esse est rerum tempore vitae
  9. sequi sint nihil reprehenderit dolor beatae ea dolores
  10. neque
  11. fugiat blanditiis voluptate porro vel nihil molestiae ut
  12. reiciendis
  13. qui aperiam non debitis possimus qui neque nisi nulla
  14. 2 1 3 ea molestias quasi exercitationem repellat qui ipsa sit et iusto sed quo iure
  15. aut voluptatem occaecati omnis eligendi aut ad
  16. voluptatem doloribus vel accusantium quis pariatur
  17. molestiae porro eius odio et labore et velit aut
  18. 3 1 4 eum et est occaecati ullam et saepe reiciendis voluptatem adipisci
  19. sit amet autem assumenda provident rerum culpa
  20. quis hic commodi nesciunt rem tenetur doloremque ipsam
  21. iure
  22. quis sunt voluptatem rerum illo velit
  23. 4 1 5 nesciunt quas odio repudiandae veniam quaerat sunt sed
  24. alias aut fugiat sit autem sed est
  25. voluptatem omnis possimus esse voluptatibus quis
  26. est aut tenetur dolor neque
  27. ━━━┷━━━━━━━━┷━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Fetch from multiple urls

Suppose you are querying several endpoints, perhaps with different query parameters and you want to view all the responses as a single dataset. You can make use of $it to run nu commands on every row of data.

An example JSON file, urls.json, with the following contents:

  1. {
  2. "urls": [
  3. "https://jsonplaceholder.typicode.com/posts/1",
  4. "https://jsonplaceholder.typicode.com/posts/2",
  5. "https://jsonplaceholder.typicode.com/posts/3"
  6. ]
  7. }
  1. > open urls.json | get urls | each { |u| fetch $u }

Output

  1. ━━━┯━━━━━━━━┯━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  2. # │ userId │ id │ title │ body
  3. ───┼────────┼────┼─────────────────────────────────────────────────────────┼──────────────────────────────────────────────────────────
  4. 0 1 1 sunt aut facere repellat provident occaecati excepturi quia et suscipit
  5. optio reprehenderit suscipit recusandae consequuntur expedita et cum
  6. reprehenderit molestiae ut ut quas totam
  7. nostrum rerum est autem sunt rem eveniet architecto
  8. 1 1 2 qui est esse est rerum tempore vitae
  9. sequi sint nihil reprehenderit dolor beatae ea dolores
  10. neque
  11. fugiat blanditiis voluptate porro vel nihil molestiae ut
  12. reiciendis
  13. qui aperiam non debitis possimus qui neque nisi nulla
  14. 2 1 3 ea molestias quasi exercitationem repellat qui ipsa sit et iusto sed quo iure
  15. aut voluptatem occaecati omnis eligendi aut ad
  16. voluptatem doloribus vel accusantium quis pariatur
  17. molestiae porro eius odio et labore et velit aut
  18. ━━━┷━━━━━━━━┷━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

If you specify the --raw flag, you’ll see 3 separate json objects, one in each row.

  1. > open urls.json | get urls | each { |u| fetch $u -r }

Output

  1. ━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  2. # │ <value>
  3. ───┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
  4. 0 {
  5. "userId": 1,
  6. "id": 1,
  7. "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
  8. "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum
  9. │ rerum est autem sunt rem eveniet architecto"
  10. }
  11. 1 {
  12. "userId": 1,
  13. "id": 2,
  14. "title": "qui est esse",
  15. "body": "est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro
  16. │ vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla"
  17. }
  18. 2 {
  19. "userId": 1,
  20. "id": 3,
  21. "title": "ea molestias quasi exercitationem repellat qui ipsa sit aut",
  22. "body": "et iusto sed quo iure\nvoluptatem occaecati omnis eligendi aut ad\nvoluptatem doloribus vel accusantium quis
  23. │ pariatur\nmolestiae porro eius odio et labore et velit aut"
  24. }
  25. ━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

To combine these responses together into a valid JSON array, you can turn the table into json.

  1. > open urls.json | get urls | each { |u| fetch $u } | to json

Output

  1. [
  2. {
  3. "userId": 1,
  4. "id": 1,
  5. "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
  6. "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
  7. },
  8. {
  9. "userId": 1,
  10. "id": 2,
  11. "title": "qui est esse",
  12. "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"
  13. },
  14. {
  15. "userId": 1,
  16. "id": 3,
  17. "title": "ea molestias quasi exercitationem repellat qui ipsa sit aut",
  18. "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"
  19. }
  20. ]

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.

  1. {
  2. "my_payload": {
  3. "title": "foo",
  4. "body": "bar",
  5. "userId": 1
  6. }
  7. }
  1. > open payload.json | get my_payload | to json | post https://jsonplaceholder.typicode.com/posts $in

Output

  1. ━━━━━
  2. id
  3. ─────
  4. 101
  5. ━━━━━

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.

  1. > open urls.json | get urls | first | fetch $in | upsert id {|item| $item.id | inc} | to json | post https://jsonplaceholder.typicode.com/posts $in
  1. ━━━━━
  2. id
  3. ─────
  4. 101
  5. ━━━━━