I have put together an API that responds to a POST request by putting the content of the new resource in the response body, and the URL of the new resource in the Location HTTP response header.
Sample request:
POST /api/v1/widgets HTTP/1.1
Content-type: application/json;
Accept: application/json;
{
"name": "hugo@example.com",
"price": "10",
}
Sample response:
HTTP 201 Created
Location: http://example.com/api/v1/widgets/123456
{
'widget':
{
'id': "123456",
'created': "2012-06-22T12:43:37+0100",
'name': "hugo@example.com",
'price': "10",
},
}
Someone has raised an issue that the URL should also be in the body of the response. Is there a best practice on this?
I would put it in the header (as Location: http://blah.blah.com/blah). You could put it in your body as well if you want (in whatever appropriate format you are sending), and it wouldn’t be improper.
The atompub REST API is usually a good reference for a good REST API. They put it in both.