Test async API jobs: 202 Accepted, polling and the status endpoint

Some work takes too long for one HTTP request. The API answers 202 Accepted with a job id, and the client asks later whether it's done. Testing that means testing three things: the start, the waiting, and the result.

The start request

HTTP
POST {{baseUrl}}/exports
Content-Type: application/json

{"type": "orders", "from": "2026-11-01", "to": "2026-11-23", "format": "csv"}
JavaScript
pm.test('export accepted', () => pm.response.to.have.status(202));

const job = pm.response.json();
pm.test('job has an id and a status', () => {
  pm.expect(job.id).to.be.a('string');
  pm.expect(job.status).to.be.oneOf(['queued', 'running']);
});

pm.test('Location points at the job', () => {
  pm.expect(pm.response.headers.get('Location')).to.include(job.id);
});

pm.environment.set('jobId', job.id);

A 200 with the finished result is fine for small exports, but then it's not async, and the client code must handle both. Decide which one the API promises.

The job endpoint

HTTP
GET {{baseUrl}}/exports/{{jobId}}
JavaScript
const job = pm.response.json();
pm.test('status is a known value', () => {
  pm.expect(job.status).to.be.oneOf(['queued', 'running', 'succeeded', 'failed']);
});

if (job.status === 'succeeded') {
  pm.test('finished job has a download link', () => pm.expect(job.resultUrl).to.match(/^https:\/\//));
}
if (job.status === 'failed') {
  pm.test('failed job says why', () => pm.expect(job.error).to.be.a('string'));
}

Many APIs also send Retry-After on the job endpoint while it's running. If yours does, the polling loop below can use it.

Polling

The collection runner sends each request once, in order. A fixed Delay between requests works if the job always takes the same time, which it doesn't. Poll from the shell instead, reading the status from the JSON output:

Terminal
#!/usr/bin/env bash
set -euo pipefail

# Start the export and keep the job id
JOB_ID=$(requestarium run "Exports/Start export" --env Staging --json | jq -r '.response.body | fromjson | .id')

# Poll for up to 5 minutes
for i in $(seq 1 60); do
  STATUS=$(requestarium run "Exports/Get export" --env Staging --var jobId="$JOB_ID" --json \
    | jq -r '.response.body | fromjson | .status')
  echo "attempt $i: $STATUS"
  case "$STATUS" in
    succeeded) break ;;
    failed) echo "export failed"; exit 1 ;;
  esac
  sleep 5
done

[ "$STATUS" = "succeeded" ] || { echo "export did not finish in time"; exit 1; }

# Check the result with its own assertions
requestarium run "Exports/Download export" --env Staging --var jobId="$JOB_ID"

run --json prints one request result; the response body is in .response.body as text, so fromjson parses it. Values your scripts set are in .variablesSet too. The JSON output article walks through the format.

The result

The download request holds the checks for what the job produced:

JavaScript
pm.test('CSV has a header row', () => {
  pm.expect(pm.response.text().split('\n')[0]).to.equal('orderId,createdAt,total,currency');
});

See testing CSV export endpoints for more on checking file contents.

Cases worth testing

  • Status never goes backwards: log each status in the loop and fail if succeeded is followed by running.
  • Someone else's job: GET /exports/{{jobId}} with another user's token must be 404. Job ids are often guessable.
  • Cancel: if there's DELETE /exports/{{jobId}}, a cancelled job should end as cancelled, not succeeded.
  • Expired results: download links usually expire. Check the link's lifetime matches the docs.
  • Same request twice: two identical export requests can return the same job or two. Either is fine if it's documented; see idempotency keys.