Skip to content

Instantly share code, notes, and snippets.

@alganet
Last active December 22, 2015 06:19
Show Gist options
  • Save alganet/6430420 to your computer and use it in GitHub Desktop.
Save alganet/6430420 to your computer and use it in GitHub Desktop.
RESTful HTTP Containers. 01 - A GET on a container. 02 - A specific GET on a container with a filter (defined by the stateless forms in the request before). 03 - A GET on an item (defined by stateless links of the request before). 04 - A POST to create two new items (or any ammount, defined by the forms in requests before). 05 - A POST to update…
GET /products HTTP/1.1
Host: example.com
HTTP/1.1 200 Ok
Content-Type: text/html;charset=utf-8
<!doctype html>
<title>All Products</title>
<h1>Products</h1>
<ul>
<li><a rel="item" href="/products/1">Product 1</a></li>
<li><a rel="item" href="/products/2">Product 2</a></li>
<li><a rel="item" href="/products/3">Product 3</a></li>
<li><a rel="item" href="/products/4">Product 4</a></li>
</ul>
<form method=get action=/products>
<fieldset>
<legend>Filter products</legend>
<label>IDs separated by commas <input name=id value="1,4"/></label>
<button type=submit>Filter</button>
</fieldset>
</form>
<form method=post action=/products>
<fieldset>
<legend>New Product</legend>
<label>Name <input name="product[][name]"/></label>
<label>Yadda Text <input name="product[][yadda]"/></label>
<button type=submit>Create New Product</button>
</fieldset>
</form>
GET /products?id=1,4 HTTP/1.1
Host: example.com
HTTP/1.1 200 Ok
Content-Type: text/html;charset=utf-8
<!doctype html>
<title>All Products</title>
<h1>Products</h1>
<ul>
<li><a rel="item" href="/products/1">Product 1</a></li>
<li><a rel="item" href="/products/4">Product 4</a></li>
</ul>
<form method=get action=/products>
<fieldset>
<legend>Filter products</legend>
<label>IDs separated by commas <input name=id value="1,4"/></label>
<button type=submit>Filter</button>
</fieldset>
</form>
<form method=post action=/products>
<fieldset>
<legend>New Product</legend>
<label>Name <input name="product[][name]"/></label>
<label>Yadda Text <input name="product[][yadda]"/></label>
<button type=submit>Create New Product</button>
</fieldset>
</form>
GET /products/1 HTTP/1.1
Host: example.com
HTTP/1.1 200 Ok
Content-Type: text/html;charset=utf-8
<!doctype html>
<title>Product 1</title>
<h1>Product 1</h1>
<nav><a rel="parent collection" href="/products">All Products</a></nav>
<p>Product 1 yaddayadda</p>
POST /products HTTP/1.1
Host: example.com
Content-Type: multipart/formdata
product[][name]=Product 5&
product[][yadda]=Product yadda yadda yadda&
product[][name]=Product 6&
product[][yadda]=Product yadda yadda yaddayadda
HTTP/1.1 303 See Other
Location: /products?id=5,6
POST /products?id=1,2 HTTP/1.1
Host: example.com
Content-Type: multipart/formdata
yadda=Products 1 and 2 just yadda yadda yadda
HTTP/1.1 303 See Other
Location: /products?id=1,2
POST /products?id=1 HTTP/1.1
Host: example.com
Content-Type: multipart/formdata
yadda=Yadda for 1 now just yadda yadda
HTTP/1.1 303 See Other
Location: /products/1
POST /products?id=1,4,5 HTTP/1.1
Host: example.com
active=false
HTTP/1.1 200 No Content
Content-Type: text/html;charset=utf-8
<!doctype html>
<title>No Products</title>
<h1>No Products</h1>
<form method=get action=/products>
<fieldset>
<legend>Filter products</legend>
<label>IDs separated by commas <input name=id value="1,4,5"/></label>
<button type=submit>Filter</button>
</fieldset>
</form>
<form method=post action=/products>
<fieldset>
<legend>New Product</legend>
<label>Name <input name="product[][name]"/></label>
<label>Yadda Text <input name="product[][yadda]"/></label>
<button type=submit>Create New Product</button>
</fieldset>
</form>
POST /products?id=1,4,5,20009 HTTP/1.1
Host: example.com
active=false
HTTP/1.1 409 Conflict
Content-Type: text/html;charset=utf-8
<!doctype html>
<title>Error Deleting Products</title>
<h1>Error Deleting Products</h1>
<form method=get action=/products>
<fieldset>
<legend>Deleting products</legend>
<p>The product with id 20009 could not be found. Please check the ID again and resubmit the form</p>
<label>IDs separated by commas <input name=id value="1,4,5,20009"/></label>
<button type=submit>Delete</button>
</fieldset>
</form>
</form>
@augustohp
Copy link

On 07.http I have always some doubts on what to do with the post result:

  1. You returned a 204 and gave a content, there isn't any change the client may ignore the content you just gave?
  2. And what does "No content" actually means? That the post was actually successful or that you could not find those resources? What would be a nice alternative to the other case?
  3. If just one of the product IDs does not exist, what would be the expected way to treat it in your opinion?

@alganet
Copy link
Author

alganet commented Sep 4, 2013

@augustohp

I've changed for 200 =) 204 Means means that an operation on a resource has been successful and there is no content to display for that resource.

That is actually what happened on 07.http and 204 is semantically appropriate, the only problem is that by the spec a 204 should not return an entity body, so I've changed for 200.

I've also added a 08.http with an update for an item that doesn't exists on a collection.

@raphapy
Copy link

raphapy commented Sep 5, 2013

Consider the following example :

PUT
/someresourcepath?condition=<logical_sentence>

In some cases I can need to make a request like this to transfer the same "state" to all resources that meet the «logical condition»
(in the request body is the representation of the new state to transfer for resources).

Is it right ? Any suggestions ?

@danizord
Copy link

danizord commented Dec 1, 2013

@alganet If I want to move the "creation form" to its own page, which should be the URL?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment