Created by olivier morel / @algorithme
Method | Safe? | Idempotent? | Main usages |
---|---|---|---|
GET | YES | YES | retrieve a resource |
HEAD | YES | YES | retrieve a resource metadata only, check for existence |
OPTIONS | YES | YES | retrieve supported methods for a resource |
PUT | NO | YES | replace or create a resource |
DELETE | NO | YES | delete a resource |
POST | NO | NO | other actions |
# GET request for a simple resource
GET /user/1 HTTP/1.1
Host: api.example.net
# No body
# GET response
HTTP/1.1 200 OK
Content-Type: application/json;charset=UTF-8
# Body
{ "id": "urn:user:1",
"username": "algorithme",
"email": "olivier@example.net" }
# HEAD request for a simple resource
HEAD /article/1 HTTP/1.1
Host: api.example.net
# No body
# HEAD response
HTTP/1.1 200 OK
Content-Type: application/json;charset=UTF-8
Last-Modified: Sun, 29 Mar 2009 08:00:00 GMT
# No body
# PUT request to create a new resource with resource identifier
PUT /user/1/article/1 HTTP/1.1
Host: api.example.net
# Body
{ "title": "My first article"
"body": "..." }
# PUT response
HTTP/1.1 201 Created
Content-Type: application/json;charset=UTF-8
Last-Modified: Sun, 29 Mar 2009 08:00:00 GMT
# Body
{ "id": "urn:user:1:article:1"
"title": "My first article"
"body": "..." }
# PUT request to update a resource completely
PUT /user/1/article/1 HTTP/1.1
Host: api.example.net
# Body
{ "title": "My first article"
"body": "Ok I'll write some text" }
# PUT response
HTTP/1.1 200 OK
Content-Type: application/json;charset=UTF-8
Last-Modified: Sun, 29 Mar 2009 08:00:00 GMT
# Body
{ "id": "urn:user:1:article:1"
"title": "My first article"
"body": "Ok I'll write some text" }
# DELETE request
DELETE /user/1/article/1 HTTP/1.1
Host: api.example.net
# No Body
# PUT response
HTTP/1.1 204 No Content
# No Body
# POST request to create a new resource
POST /user/1/task HTTP/1.1
Host: api.example.net
# Body
{ "name": "prepare my slides"
"priority": 10 }
# POST response
HTTP/1.1 201 Created
Location: http://api.example.net/user/1/task/1
Content-Location: http://api.example.net/user/1/task/1
Content-Type: application/json;charset=UTF-8
{ "id": "urn:user:1:task:1"
"name": "prepare my slides"
"priority": 10 }
# POST request to modify part of the resource
POST /user/1/article/1/title HTTP/1.1
Host: api.example.net
# Body
a new name
# PUT response
HTTP/1.1 303 See Other
Location: http://api.example.net/user/1/article/1
# No Body
# POST request to initiate the task
POST /video/conversion/convert_to_mp4 HTTP/1.1
Host: api.example.net
Content-Type: application/json;charset=UTF-8
# Body
{ "src": "https://youtu.be/Kbwk2vwXNyU" }
# POST response with newly created task
HTTP/1.1 202 Accepted
Content-Location: http://api.example.net/video/conversion/task/1
Content-Type: application/json;charset=UTF-8
Date: Sun, 13 Sep 2009 01:49:27 GMT
# Body
{ "id": "urn:video:conversion:task:1",
"status": "pending",
"self": { "href": "http://api.example.net/video/conversion/task/1" },
"message": "Your request has been accepted and is being processed.",
"ping-after": "2009-09-13T01:49:42Z" }
# After a while
# GET request to check the state of the processing
GET /video/conversion/task/1 HTTP/1.1
Host: api.example.net
# No body
# GET response with new state of the processing
HTTP/1.1 200 OK
Content-Type: application/json;charset=UTF-8
# Body
{ "id": "urn:video:conversion:task:1",
"status": "pending",
"self": { "href": "http://api.example.net/video/conversion/task/1" },
"message": "Your request is being processed.",
"ping-after": "2009-09-13T01:49:57Z" }
# After a while
# GET request to check the state of the processing
GET /video/conversion/task/1 HTTP/1.1
Host: api.example.net
# No body
# GET response with the result of the processing
HTTP/1.1 303 See Other
Location: http://api.example.net/video/youtube/1
Content-Location: http://api.example.net/video/youtube/1
# Body
{ "id": "urn:video:conversion:task:1",
"status": "done",
"self": { "href": "http://api.example.net/video/conversion/task/1" },
"message": "Your request has been successfully processed." }
# DELETE request to initiate the task
DELETE /user/1 HTTP/1.1
Host: api.example.net
Content-Type: application/json;charset=UTF-8
# No Body
# DELETE response with newly created task
HTTP/1.1 202 Accepted
Content-Location: http://api.example.net/user/task/1
Content-Type: application/json;charset=UTF-8
Date: Sun, 13 Sep 2009 01:49:27 GMT
# Body
{ "id": "urn:user:task:1",
"status": "pending",
"self": { "href": "http://api.example.net/user/task/1" },
"message": "Your request has been accepted and is being processed.",
"ping-after": "2009-09-13T01:49:42Z" }
# After a while
# GET request to check the state of the processing
GET /user/task/1 HTTP/1.1
Host: api.example.net
# No body
# GET response with new state of the processing
HTTP/1.1 200 OK
Content-Type: application/json;charset=UTF-8
# Body
{ "id": "urn:user:task:1",
"status": "done",
"self": { "href": "http://api.example.net/user/task/1" },
"message": "Your request has been processed.",
"ping-after": "2009-09-13T01:49:57Z" }
Code | Message | Description |
---|---|---|
4xx | Client Inputs Error | |
400 | Bad Request | general code |
403 | Forbidden | not allowed to access |
404 | Not Found | resource does not exist (or not allowed to access) |
405 | Not Allowed | http method not handled |
5xx | Server Error | |
500 | Internal Server Error | something somewhere went wrong |
503 | Service Unavailable | temporary not available (Retry-After) |
# HTTP Error Header
HTTP/1.1 403 Forbidden
Content-Type: application/json;charset=UTF-8
Content-Language: en
Date: Wed, 14 Oct 2009 10:16:54 GMT
Link: <http://api.example.net/user/errors/unauthorized_access.html>;rel="help"
# HTTP body
{ "message": "User is not authorized to other users profile.",
"error-id": "error-access-unauthorized-profile",
"rel": { "href": "http://api.example.net/user/2" }
}
try {
response = httpRequest.send("GET ...")
if (response.code >= 200 && response.code < 400) {
// Success handle the success based on the action
...
} else if (response.code == 500) {
// Server error
// Try to work without a server for a while :)
...
} else if (response.code == 503) {
// Temporary server error
delay = response.header.retry_after
wait(delay)
// Wait delay and then retry
} else if (response.code >= 400) {
// Error in the input, there should be an implementation error in the client
// You could retry, after modifying some parameters or methods
}
} catch (NetworkFailure failure) {
// Retry now or with a delay
...
}
{"name": "olivier", "alternate": "http://api.example.net/user/dump/1"}
Link: <http://api.example.net/user/dump/1>;rel=alternate
GET /events?after=2015-01-01&before=2015-12-31&sortBy=date&limit=10
# GET response HEADER
HTTP/1.1 200 OK
Content-Type: application/json;charset=UTF-8
Cache-Control: max-age=60
Content-Language: en
# GET response BODY
{ "total": 50,
"events": [
{ "title": "...", "date": "...", "link": {
"rel": "http://api.example.net/rels/event",
"href": "http://api.example.net/event/1",
}},
...
],
"next": "http://api.example.net/events?after=2015-01-01&before=2015-12-31&sortBy=date&limit=10&start=10",
"last": "http://api.example.net/events?after=2015-01-01&before=2015-12-31&sortBy=date&limit=10&start=40"
}
- http://api.example.net/events?view=last_month- http://api.example.net/events?view=every-friday
- Accept: application/json;q=1.0, application/html;q=0.6, */*;q=0.0
- Accept-Language: fr;q=1.0, en;q=0.5
- Accept-Encoding: gzip
- http://fr.example.net/article/1
- http://api.example.net/article/1?format=json
Created by olivier morel / @algorithme