Skip to content

cURL command-line

One-liners for every endpoint plus jq tricks for piping the response into your shell.

cURL is the common tool for API debugging. Every API doc page has cURL examples for a reason: they work in any shell, they show exactly what is sent and received, and the output is plain text you can pipe into anything. This guide is a list of recipes you can copy and adapt for the GoRest endpoints, plus the jq filters that turn raw JSON into something readable.

Set up the token

Put the token in an environment variable so you do not paste it into your shell history. From here on, every example uses $TOKEN for the bearer token. Reads work without a token too, but then you see only the sample data.

export TOKEN=YOUR_TOKEN # from /my/tokens
export API=https://gorest.co.in/public/v3

Brackets in a URL, like page[size], are special to cURL. Add -g to every call that has them, as the examples below do.

List resources

The read endpoints follow the same shape: /users, /posts, /comments, /tasks. A list comes back as {"data": [...], "meta": {...}}. Pipe through jq to read it in a terminal:

curl -sSg -H "Authorization: Bearer $TOKEN" \
     "$API/users?filter[status]=active&page[size]=5" | jq '.data[0]'

curl -sSg -H "Authorization: Bearer $TOKEN" \
     "$API/posts?page[size]=5" | jq '.data[].title'

The -sS combination is the one to remember: -s hides the progress meter, -S turns error reporting back on (so the script still fails loudly when something is wrong). Without -s you cannot pipe to jq cleanly, because cURL prints the progress bar to stderr and it still messes up the visible output.

Page through a list

A page holds 25 rows by default. Ask for 1 to 100 with page[size]. When meta.has_more is true, pass meta.next_cursor as page[after] to get the next page:

CURSOR=$(curl -sSg "$API/users?page[size]=10" | jq -r '.meta.next_cursor')
curl -sSg "$API/users?page[size]=10&page[after]=$CURSOR" | jq '.data | length'

Filters use the filter[field] form. Use search for a text search and sort to order the rows (a leading - sorts descending). fields[users] returns only the fields you name, plus id.

curl -sSg -H "Authorization: Bearer $TOKEN" \
     "$API/users?filter[email]=pari.saxena@example.com" | jq

curl -sSg -H "Authorization: Bearer $TOKEN" \
     "$API/tasks?filter[status]=done&sort=-created_at" | jq '.data[].title'

curl -sSg "$API/users?search=saxena&fields[users]=name,email" | jq '.data'

Fetch a single record

Ids are UUID strings. Take one from a list call:

ID=$(curl -sSg "$API/users?page[size]=1" | jq -r '.data[0].id')

curl -sS -H "Authorization: Bearer $TOKEN" "$API/users/$ID" | jq

# Show only the fields you care about
curl -sS -H "Authorization: Bearer $TOKEN" "$API/users/$ID" \
     | jq '{id, email, status}'

Create a record

The -d flag sets the body. Combined with -H "Content-Type: application/json", you have a full POST. A new user needs name and email; status is active unless you say otherwise. The token needs the users:write scope. Capture the new id with jq -r if you need it for follow-up calls:

NEW_ID=$(curl -sS -X POST \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Avani Iyer",
    "email": "avani-'$(date +%s)'@example.com"
  }' \
  "$API/users" | jq -r '.id')

echo "Created user $NEW_ID"

Update and delete

PATCH changes only the fields you send. There is no PUT in v3. DELETE returns 204. Note the -i flag to keep the response headers so you can check the status code:

curl -sSi -X PATCH \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"status": "inactive"}' \
  "$API/users/$NEW_ID"

curl -sSi -X DELETE \
  -H "Authorization: Bearer $TOKEN" \
  "$API/users/$NEW_ID"

Every item has an ETag. Send it back in If-Match on a PATCH or DELETE, and the call fails with 412 if someone changed the item in between.

Inspect headers

The rate-limit budget and the paging links are in headers, not the body. Use -D - to dump them to stdout, or -I for a HEAD request that fetches only the headers:

curl -sSg -D - -o /dev/null -H "Authorization: Bearer $TOKEN" \
     "$API/users?page[size]=1" | grep -i '^ratelimit\|^link'

# Just the headers, no body
curl -sSIg -H "Authorization: Bearer $TOKEN" "$API/users?page[size]=1"

Test loading and error states

v3 has two helper endpoints for this. /http/delay/N waits N milliseconds (up to 10000), and /http/status/N answers with the status code you name, so you can check your error path. A 4xx or 5xx answer is a normal Problem Details body with the code requested_status:

# Fake a 2-second slow response
time curl -sS "$API/http/delay/2000" > /dev/null

# Fake a 503
curl -sSi "$API/http/status/503"

Save and load presets

If you call the same endpoints from your shell again and again, write them as small functions in your .zshrc or .bashrc. Saves a lot of retyping:

function gorest() {
  curl -sSg -H "Authorization: Bearer $TOKEN" \
       -H "Content-Type: application/json" "$@"
}

gorest "$API/users?filter[status]=active" | jq '.data[].email'
gorest -X POST -d '{"name":"X","email":"x@e.io"}' \
       "$API/users" | jq

Tips

  • Use -w '%{http_code}' to print the status code at the end of the response. Useful in scripts that branch on success vs failure.
  • When the response is huge, pipe through jq -C for colour, then less -R to scroll without losing colour.
  • v3 answers JSON only. The old v2 API could also answer XML; see /docs/v2.
  • curl -k disables certificate verification. Never enable it permanently. If you hit cert errors against this API, the cert chain is fine; your local trust store needs updating.
Related guides

Keep going