User:Hawleyal/sandbox
Appearance
Title: RESTful HTTP applications & URI resources
Types of resources
[edit]Plural resource
[edit]- 7 basic routes
description | HTTP method | URI |
---|---|---|
list of all articles | GET | /articles |
form for creating new article | GET | /articles/new |
create new article | POST | /articles |
specific article | GET | /articles/{id} |
form for editing specific article | GET | /articles/{id}/edit |
update specific article | PATCH/PUT | /articles/{id} |
delete specific article | DELETE | /articles/{id} |
Multiple-word resource
[edit]- hyphenate name in uri
description | HTTP method | URI |
---|---|---|
list of all security groups | GET | /security-groups |
form for creating new security group | GET | /security-groups/new |
create new security group | POST | /security-groups |
specific security group | GET | /security-groups/{id} |
form for editing specific security group | GET | /security-groups/{id}/edit |
update specific security group | PATCH/PUT | /security-groups/{id} |
delete specific security group | DELETE | /security-groups/{id} |
Singular resource
[edit]- no index
description | HTTP method | URI |
---|---|---|
form for creating processor | GET | /processor/new |
create processor | POST | /processor |
processor | GET | /processor |
form for editing processor | GET | /processor/edit |
update processor | PATCH/PUT | /processor |
delete processor | DELETE | /processor |
Nested resource
[edit]- shallow nesting
- don't include parent path if not needed
description | HTTP method | URI |
---|---|---|
list of all comments for specific article | GET | /articles/{article_id}/comments |
form for creating new comment for specific article | GET | /articles/{article_id}/comments/new |
create new comment for specific article | POST | /articles/{article_id}/comments |
specific comment | GET | /comments/{id} |
form for editing specific comment | GET | /comments/{id}/edit |
update a specific comment | PATCH/PUT | /comments/{id} |
delete a specific comment | DELETE | /comments/{id} |
Resource attributes
[edit]- specific attributes on their own routes
description | HTTP method | URI |
---|---|---|
download for specific image | GET | /images/{id}/download |
xml meta data for specific image | GET | /image/{id}/xml-meta-data |
Query parameters
[edit]- filter
- sorting
- paging
- format
description | HTTP method | URI |
---|---|---|
filter list of all articles | GET | /articles?from=2012&to=2013&keywords=birthday |
sorting list of all articles | GET | /articles?sort=date+asc |
paging list of all articles | GET | /articles?start=1&max=10 |
specific article in HTML format | GET | /articles/{id} |
specific article in JSON format | GET | /articles/{id}?format=json |
specific article in XML format | GET | /articles/{id}?format=xml |
download for specific image in JPEG format | GET | /image/{id}?format=jpg |
download for specific image in PNG format | GET | /image/{id}?format=png |
Examples using HTML forms
[edit]- HTML can be made RESTful by using forms to create and update data.
- HTML includes 2 extra routes for forms to create new and edit specific resources.
- HTML forms generally use content type
application/x-www-form-urlencoded
, which must be processed. - HTML doesn't support methods
DELETE
,PATCH
, orPUT
in forms, so a hidden element must be included and processed. - HTML applications commonly use the PRG (post-redirect-get) pattern for creating, updating, and deleting records.
description | HTTP method | URI |
---|---|---|
list of all articles | GET | /articles |
form for creating new article | GET | /articles/new |
create new article | POST | /articles |
specific article | GET | /articles/{id} |
form for editing specific article | GET | /articles/{id}/edit |
update specific article | PATCH/PUT | /articles/{id} |
delete specific article | DELETE | /articles/{id} |
List of all articles
[edit]Request
GET /articles
Response
200 OK Content-Type: text/html <!DOCTYPE html><html> <head><title>Articles</title></head> <body> <h1><a href="/articles">Articles</a></h1> <div class="article"><a href="/articles/3">Article 3 by Author 3 on 2014-01-03</a></div> <div class="article"><a href="/articles/2">Article 2 by Author 2 on 2014-01-02</a></div> <div class="article"><a href="/articles/1">Article 1 by Author 1 on 2014-01-01</a></div> </body> </html>
Form for creating new article
[edit]Request
GET /articles/new
Response
200 OK Content-Type: text/html <!DOCTYPE html><html> <head><title>New article</title></head> <body> <h1><a href="/articles/new">New article</a></h1> <form method="post" action="/articles"> <div><label for="title">title</label><input id="title" name="title" type="text"/></div> <div><label for="author">author</label><input id="author" name="author" type="text"/></div> <div><label for="body"></label><textarea id="body" name="body"></textarea></div> <div><button type="submit">Create</button></div> </form> </body> </html>
Create new article
[edit]Request
POST /articles Content-Type: application/x-www-form-urlencoded title=Article+4&author=Author+4&body=Lorem+ipsum+dolor+sit+amet%2C+consectetur+adipiscing+elit.
Response
302 Found Location: /articles/4
Specific article
[edit]Request
GET /articles/1
Response
200 OK Content-Type: text/html <!DOCTYPE html><html> <head><title>Article 1</title></head> <body> <h1 class="title"><a href="/articles/1">Article 1</a></h1> <div class="author">Author 1</div> <div class="date">2014-01-01</div> <div class="body">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</div> </body> </html>
Form for editing specific article
[edit]- HTML doesn't support
PUT
method in forms, so a hidden element must be included and processed.
Request
GET /articles/1/edit
Response
200 OK Content-Type: text/html <!DOCTYPE html><html> <head><title>Edit article: Article 1</title></head> <body> <h1><a href="/articles/1/edit">Edit article: Article 1</a></h1> <form method="post" action="/articles/1"><input name="_method" value="PUT" type="hidden"/> <div><label for="title">title</label><input id="title" name="title" type="text" value="Article 1"/></div> <div><label for="author">author</label><input id="author" name="author" type="text" value="Author 1"/></div> <div><label for="body"></label><textarea id="body" name="body">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</textarea></div> <div><button type="submit">Update</button></div> </form> </body> </html>
Update specific article
[edit]- HTML doesn't support PUT method in forms, so a hidden element must be included and processed.
Request
POST /articles/1 Content-Type: application/x-www-form-urlencoded _method=PUT&title=Article+1&author=Author+1&body=Lorem+ipsum+dolor+sit+amet%2C+consectetur+adipiscing+elit.
Response
302 Found Location: /articles/1
Delete specific article
[edit]- A form for this is usually included in another resource view, for example, a specific article may include a delete form/button combo.
- HTML doesn't support
DELETE
method in forms, so a hidden element must be included and processed.
Form
<form method="post" action="/articles/1"><input name="_method" value="DELETE" type="hidden"/> <button type="submit">Delete</button> </form>
Request
POST /articles/1 Content-Type: application/x-www-form-urlencoded _method=DELETE
Response
302 Found Location: /articles
Examples using JSON API
[edit]- JSON can be used by JavaScript and other languages as a quick way to make a RESTful API.
- RESTful APIs generally don't need forms to create or update data.
- Since APIs don't use forms, they can execute methods
DELETE
,PATCH
, andPUT
directly. - APIs usually don't use the PRG (post-redirect-get) pattern.
description | HTTP method | URI |
---|---|---|
list of all articles | GET | /articles |
create new article | POST | /articles |
specific article | GET | /articles/{id} |
update specific article | PATCH/PUT | /articles/{id} |
delete specific article | DELETE | /articles/{id} |
List of all articles
[edit]Request
GET /articles
Response
200 OK Content-Type: application/json [ {"id": 3, "uri":"/articles/3", "title": "Article 3", "author": "Author 3", "date": "2014-01-03"} ,{"id": 2, "uri":"/articles/2", "title": "Article 2", "author": "Author 2", "date": "2014-01-02"} ,{"id": 1, "uri":"/articles/1", "title": "Article 1", "author": "Author 1", "date": "2014-01-01"} ]
Create new article
[edit]Request
POST /articles Content-Type: application/json { "title": "Article 4" ,"author": "Author 4" ,"body": "Lorem+ipsum+dolor+sit+amet%2C+consectetur+adipiscing+elit." }
Response
201 Created Location: /articles/4 Content-Type: application/json { "id": 4 ,"uri":"/articles/4" ,"title": "Article 4" ,"author": "Author 4" ,"date": "2014-01-04" ,"body": "Lorem+ipsum+dolor+sit+amet%2C+consectetur+adipiscing+elit." }
Specific article
[edit]Request
GET /articles/1
Response
200 OK Content-Type: application/json { "id": 1 ,"uri":"/articles/1" ,"title": "Article 1" ,"author": "Author 1" ,"date": "2014-01-01" ,"body": "Lorem+ipsum+dolor+sit+amet%2C+consectetur+adipiscing+elit." }
Update specific article
[edit]Request
PUT /articles/1 Content-Type: application/json { "id": 1 ,"uri":"/articles/1" ,"title": "Article 1" ,"author": "Author 1" ,"date": "2014-01-01" ,"body": "Lorem+ipsum+dolor+sit+amet%2C+consectetur+adipiscing+elit." }
Response
201 Created Content-Type: application/json { "id": 1 ,"uri":"/articles/1" ,"title": "Article 1" ,"author": "Author 1" ,"date": "2014-01-01" ,"body": "Lorem+ipsum+dolor+sit+amet%2C+consectetur+adipiscing+elit." }
Delete specific article
[edit]Request
DELETE /articles/1
Response
204 No Content