HTTP

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

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

    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. }
    17. 2 {
    18. "userId": 1,
    19. "title": "ea molestias quasi exercitationem repellat qui ipsa sit aut",
    20. "body": "et iusto sed quo iure\nvoluptatem occaecati omnis eligendi aut ad\nvoluptatem doloribus vel accusantium quis
    21. │ pariatur\nmolestiae porro eius odio et labore et velit aut"
    22. }
    23. ━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

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

    Output


    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
    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. ━━━━━
    2. id
    3. ─────
    4. ━━━━━