-
Notifications
You must be signed in to change notification settings - Fork 302
/
2012-12-13-asynchroneous-operations.html
44 lines (35 loc) · 1.75 KB
/
2012-12-13-asynchroneous-operations.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
---
layout: recipe
title: Asynchronous operations
category: Resources
author: joshua thijssen
author_email: jthijssen@noxlogic.nl
---
<h2 class="question">How can I let users create resources that might take a considerable amount of time. I cannot have my users wait on the API to finish.</h2>
<p>Instead of creating the actual resources, create a temporary one. Instead of returning a [201] (Created) HTTP response, you can then issue at
[202] (Accepted) response code. This informs the client that the request has been accepted and understood by the server, but the resource is not (yet) created.
Send the temporary resource inside the Location header.</p>
<h3>Request:</h3>
{% highlight xml %}
POST /blogs HTTP/1.1
<xml>
blogdata
</xml>
{% endhighlight %}
<h3>Response:</h3>
{% highlight xml %}
HTTP/1.1 202 Accepted
Location: /queue/12345
{% endhighlight %}
<p>This location can store information about the status of the actual resource: an ETA on when it will be created, what is currently being done or processed.</p>
<p>When the actual resource has been created, the temporary resources can return a [303] (see other) response. The location header returns the URI to the definitive
resource. A client can either [DELETE] the temporary resource, or the server can expire this resource and return a [410] GONE later on.</p>
<h3>See also</h3>
<ul>
<li><a href="http://www.adayinthelifeof.nl/2011/06/02/asynchronous-operations-in-rest/">A day in the life of - Asynchronous operations in REST</a></li>
</ul>
<h3>Caveats</h3>
<ul>
<li>Don't use a [301] or [302] when a resource has been created. These codes tell the client that the SAME resource can be found at another location. A [303] tells
a client that ANOTHER resource can be found at ANOTHER location.</li>
</ul>