On Github algorithme / restful_or_restless
Created by olivier morel / @algorithme
# 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" }
# 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 ... }
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" }
Created by olivier morel / @algorithme