POST json data with cURL from a while loop - bash shell

6,386

If you have you output in a file called tmp.json use jq to get the list of ids, one per line and then with a simple for loop make a post to your api

for i in `cat tmp.json  | jq .glossary[].id`; do 
   curl -X POST http://host/api/$i"
done
Share:
6,386

Related videos on Youtube

rSh
Author by

rSh

Updated on September 18, 2022

Comments

  • rSh
    rSh over 1 year

    I have a JSON output from which I need to extract an ID and Iterate through them and send multiple request to API execute REST API with curl. For example:

    This is how the JSON output looks like:

    {
        "glossary": [
            {
                "Title": "example glossary1",
                "id": 1,
                "description": "Hello Glossary1"
            },
            {
                "Title": "example glossary2",
                "id": 2,
                "description": "Hello Glossary2"
            },
            {
                "Title": "example glossary3",
                "id": 3,
                "description": "Hello Glossary3"
            },
            {
                "Title": "example glossary4",
                "id": 4,
                "description": "Hello Glossary4"
            }
        ]
    }
    

    The shell script should loop through this JSON file, extract the ID and loop through and execute REST API calls with CURL.

    Here is example:

    for (( i = 0 ; i < ${#id[@]} ; i++ ))
    do 
         POST REST API 
    done