Using long polling

Using long polling

In some parts of our API we implement Long Polling to handle long requests, such as creating a zip from a folder.

To demonstrate this, we will now zip the folder we created in our „Uploading a File“ How-To.

Step 1

Request:

Request the zipping of the folder.

GET /rest/folders/225849/zip

Response:

The original request will return with status code 303 and as such your framework/browser should redirect to the new endpoint.

If this does not happen you can either extract the new location from the „location“ http header or find it in the body.

The response from the actual endpoint is the following. We now know that we have started a zip job with id 225880 and we polled 1 time (The initial request).

Now we can poll for the result.

View source

    {
     "id": 225882,
     "numberOfPolls": 1,
     "creationDate": "1456145845983",
     "lastAccessDate": "1456145846424",
     "jobState": "RUNNING",
     "jobType": "ZIP",
     "pollingEndpoint": "/rest/jobs/zip",
     "pollingUrl": "/rest/jobs/zip/225882",
     "zipResult": null,
     "type": "zipPollingJob"
    }

Step 2

Request:

Poll the status.

GET /rest/jobs/zip/225882

Response:

The response will have RUNNING as long the job is still running.

If the job is done the status will be DONE. If it failed status will be FAILURE.

The result of the zip job can be found in the zipResult. We can now download the zip as a normal file with the returned id.

The result field is different for every kind of polling job.

Poll

{
 "id": 225882,
 "numberOfPolls": 3,
 "creationDate": "1456145845983",
 "lastAccessDate": "1456145861412",
 "jobState": "RUNNING",
 "jobType": "ZIP",
 "pollingEndpoint": "/rest/jobs/zip",
 "pollingUrl": "/rest/jobs/zip/225882",
 "zipResult": null,
 "type": "zipPollingJob"
}

Poll

{
 "id": 225882,
 "numberOfPolls": 0,
 "creationDate": "1456145845983",
 "lastAccessDate": null,
 "jobState": "DONE",
 "jobType": "ZIP",
 "pollingEndpoint": "/rest/jobs/zip",
 "pollingUrl": "/rest/jobs/zip/225882",
 "zipResult": 225883,
 "type": "zipPollingJob"
}

Step 3

Request:

Download the zip file.

GET /rest/files/225883/download

Response:

You will receive standard download request with headers to raise a download event.

Content-Disposition: attachment;filename=“dow$$$2134234324.zip“

Content-Type: application/zip;charset=UTF-8

Use your framework to store the response binary stream.