User:Hawleyal/sandbox

Title: RESTful HTTP applications & URI resources

Plural resource

 * 7 basic routes

Multiple-word resource

 * hyphenate name in uri

Singular resource

 * no index

Nested resource

 * shallow nesting
 * don't include parent path if not needed

Resource attributes

 * specific attributes on their own routes

Query parameters

 * filter
 * sorting
 * paging
 * format

Examples using HTML forms

 * 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, which must be processed.
 * HTML doesn't support methods,  , or   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.

List of all articles
Request GET /articles

Response 200 OK Content-Type: text/html

<!DOCTYPE html> Articles Articles Article 3 by Author 3 on 2014-01-03 Article 2 by Author 2 on 2014-01-02 Article 1 by Author 1 on 2014-01-01

Form for creating new article
Request GET /articles/new

Response 200 OK Content-Type: text/html

<!DOCTYPE html> New article New article  title  author   Create

Create new article
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
Request GET /articles/1

Response 200 OK Content-Type: text/html

<!DOCTYPE html> Article 1 Article 1 Author 1 2014-01-01    Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Form for editing specific article

 * HTML doesn't support  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> Edit article: Article 1 <a href="/articles/1/edit">Edit article: Article 1</a> <form method="post" action="/articles/1"><input name="_method" value="PUT" type="hidden"/> title <input id="title" name="title" type="text" value="Article 1"/> author <input id="author" name="author" type="text" value="Author 1"/> <textarea id="body" name="body">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Update

Update specific article

 * 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

 * 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  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"/> Delete

Request POST /articles/1 Content-Type: application/x-www-form-urlencoded

_method=DELETE

Response 302 Found Location: /articles

Examples using JSON API

 * 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,  , and   directly.
 * APIs usually don't use the PRG (post-redirect-get) pattern.

List of all articles
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
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
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
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
Request DELETE /articles/1

Response 204 No Content