Friday, October 28, 2022

[SOLVED] How to delay curl request

Issue

I'd like to add sleep to this request so as not to stress the server with too many requests at a go. I've tried adding sleep but I don't get the expected behaviour. The help is appreciated.

xargs -I{} curl --location --request POST 'https://g.com' \
--header 'Authorization: Bearer cc' \
--header 'Content-Type: application/json' \
--data-raw '{
    "c_ids": [
        "{}"
    ]
}' '; sleep 5m' < ~/recent.txt

Solution

Escaping arbitrary strings into valid JSON is a job for jq.

If you don't have a particular reason to define the curl args outside your loop:

while IFS= read -r json; do
  curl \
    --location --request POST 'https://g.com' \
    --header 'Authorization: Bearer cc' \
    --header 'Content-Type: application/json' \
    --data-raw "$json"
  sleep 5m
done < <(jq -Rc '{"c_ids": [ . ]}' recent.txt)

...or if you do:

curl_args=(
  --location --request POST 'https://g.com' \
  --header 'Authorization: Bearer cc' \
  --header 'Content-Type: application/json' \
)

while IFS= read -r json; do
  curl "${curl_args[@]}" --data-raw "$json"
  sleep 5m
done < <(jq -Rc '{"c_ids": [ . ]}' recent.txt)


Answered By - Charles Duffy
Answer Checked By - Pedro (WPSolving Volunteer)