Simulate an async job that calls back
Before you begin
- Plan
- Included in Pro and Team. How plans and limits work
- Role
- Admin or higher.
- You need
- A project, and a receiver for the callback such as the project's inbox
Many APIs answer POST /jobs with 202 Accepted and a job id, then call the callback_url from the request when the work is done. This recipe reproduces that on https://orders-api.mockhub.dev: the callback carries the id the mock generated, and goes to the address each request names.
-
Create a mock for
POST /jobswith the status202and a templated body:{ "jobId": "{{random.uuid}}", "status": "processing" }See Dynamic responses.
-
Open the project’s Webhooks tab and select New webhook. Name it
Job done. -
Under What fires it, choose When a mock is served, pick the
POST /jobsmock and set Wait before calling (seconds) to5. -
Set Send it to to The address the request gives. Under Read it from choose Body field, and enter
callback_url. -
Under Allowed hosts, select Add host and enter the host your receiver runs on, such as
hooks.acme.test. Add at least one host even if a request may name the project’s inbox; the inbox itself needs no matching entry. To always send to the inbox, set Send it to to A fixed address and select Send to this project’s inbox instead. -
Write the Body template:
{ "jobId": "{{response.body.jobId}}", "status": "done", "at": "{{firedAt}}" } -
Select Create webhook, then send a request:
Terminal window curl -X POST https://orders-api.mockhub.dev/jobs \-H 'Content-Type: application/json' \-d '{"callback_url": "https://hooks.acme.test/jobs"}'
Result
Section titled “Result”The caller gets 202 with a job id at once. Five seconds later https://hooks.acme.test/jobs receives the same id with "status": "done". Under Deliveries the delivery reads From the request beside its address.
Limits
Section titled “Limits”- The
callback_urlhost must be listed under Allowed hosts, or the delivery is Failed. See A webhook or alert was never delivered. - The callback reads at most 256 KiB of the mock’s answer.
- A webhook that sends to the address a request gives cannot be fired by hand; send a request to the mock instead.
- The mock does not track jobs: a later
GET /jobs/{id}answers what its own mock says, whatever the callback reported.