rest - Create request with POST, which response codes 200 or 201 and content - Stack Overflow
125 captures
08 Mar 2014 - 22 Feb 2026
Sep
OCT
Nov
11
2015
2016
2017
success
fail
About this capture
COLLECTED BY
Organization:
Internet Archive
These crawls are part of an effort to archive pages as they are created and archive the pages that they refer to. That way, as the pages that are referenced are changed or taken from the web, a link to the version that was live when the page was written will be preserved.
Then the Internet Archive hopes that references to these archived pages will be put in place of a link that would be otherwise be broken, or a companion link to allow people to see what was originally intended by a page's authors.
The goal is to
fix all broken links on the web
Crawls of supported "No More 404" sites.
Collection:
Wikipedia Near Real Time (from IRC)
This is a collection of web page captures from links added to, or changed on, Wikipedia pages. The idea is to bring a reliability to Wikipedia outlinks so that if the pages referenced by Wikipedia articles are changed, or go away, a reader can permanently find what was originally referred to.
This is part of the Internet Archive's attempt to
rid the web of broken links
TIMESTAMPS
The Wayback Machine - https://web.archive.org/web/20161011010658/https://stackoverflow.com/questions/1860645/create-request-with-post-which-response-codes-200-or-201-and-content
current community
chat
Stack Overflow
Meta Stack Overflow
your communities
or
to customize your list.
more stack exchange communities
company blog
Stack Exchange
Inbox
Reputation and Badges
tour
help
Tour
Start here for a quick overview of the site
Detailed answers to any questions you might have
Meta
Discuss the workings and policies of this site
Learn more about Stack Overflow the company
Business
Learn more about hiring developers or posting ads with us
Stack Overflow
Questions
Jobs
Documentation
Tags
Users
Badges
Ask Question
Dismiss
Join the Stack Overflow Community
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Create request with POST, which response codes 200 or 201 and content
up vote
50
down vote
favorite
20
Suppose I write a REST service whose intent is to add a new data item to a system.
I plan to POST to
Suppose that works, what response code should I use? And what content might I return.
I'm looking at the
definitions
of HTTP response codes and see these possibilities:
200: Return
an entity describing or containing the result of the action;
201: which means CREATED. Meaning *The request has been fulfilled and resulted in a new resource being created. The newly created resource can be referenced by the URI(s) returned in the entity of the response, with the most specific URI for the resource given by a Location header field. The response SHOULD include an entity containing a list of resource characteristics and location(s) from which the user or user agent can choose the one most appropriate. The entity format is specified by the media type given in the Content-Type header field. *
The latter sounds more in line with the Http spec, but I'm not at all clear what
The response SHOULD include an entity
containing a list of resource
characteristics and location(s)
means.
Recommendations? Interpretations?
rest
content-type
http-status-codes
share
improve this question
edited
Jan 20 '15 at 19:54
Mark Stewart
737
25
asked
Dec 7 '09 at 15:20
djna
42.3k
53
83
add a comment
6 Answers
active
oldest
votes
up vote
12
down vote
accepted
It's just a colon delimited key-value.
ETag: "xyzzy"
It can be any type of text data - I generally include a JSON string with the identifier of the item created. The ease of testing alone makes including it worthwhile.
ETag: "{ id: 1234, uri: 'http://domain.com/comments/1234', type: 'comment' }"
In this example, the identifier, the uri, and type of the created item are the "resource characteristics and location".
share
improve this answer
edited
Dec 27 '09 at 5:46
answered
Dec 26 '09 at 6:42
tempire
1,702
11
11
You are saying that an ETag corresponds to an
entity containing a list of resource characteristics and location(s)
. I can see that your suggestion is good, very much agree with your point about testing. However I don't see how this fits with "a list of resource characteristics and locations".
djna
Dec 26 '09 at 8:22
The "list of resource characteristics and locations" would be the content of whatever data structure provided. A more strict implementation would be for the JSON structure to include the resource uri and maybe the type of resource that was created. I'll adjust the answer as such.
tempire
Dec 27 '09 at 1:20
Specify the issues, so that people may learn. Otherwise, the comment is just hand-waving.
tempire
Oct 21 '13 at 23:00
@SimonGibbs What issues?
MEMark
Jul 4 '14 at 17:41
While it's strictly correct per the spec, it recommends a highly unusual implementation option. Also it does not actually answer the question at the top of the page (or else it does so by mixing up the words ETag and entity). The answer with 43 votes is probably better.
Simon Gibbs
Jul 8 '14 at 15:19
add a comment
up vote
66
down vote
I think
atompub REST API
is a great example of a restful service. See the snippet below from the atompub spec:
POST /edit/ HTTP/1.1
Host: example.org
User-Agent: Thingio/1.0
Authorization: Basic ZGFmZnk6c2VjZXJldA==
Content-Type: application/atom+xml;type=entry
Content-Length: nnn
Slug: First Post
The server signals a successful creation with a status code of 201. The response includes a Location header indicating the Member Entry URI of the Atom Entry, and a representation of that Entry in the body of the response.
HTTP/1.1 201 Created
Date: Fri, 7 Oct 2005 17:17:11 GMT
Content-Length: nnn
Content-Type: application/atom+xml;type=entry;charset="utf-8"
Location: http://example.org/edit/first-post.atom
ETag: "c180de84f991g8"
href="http://example.org/edit/first-post.atom"/>
The Entry created and returned by the Collection might not match the Entry POSTed by the client. A server MAY change the values of various elements in the Entry, such as the atom:id, atom:updated, and atom:author values, and MAY choose to remove or add other elements and attributes, or change element content and attribute values.
share
improve this answer
answered
Dec 27 '09 at 5:58
Chandra Patni
11.9k
39
55
Returning the created resource may be a bit much, if the resource is in the gigabytes magnitude...
Tor Valamo
Dec 27 '09 at 6:04
Agreed! That's the optimization of necessity-- but you don't want to do it prematurely. It's important to design in Restful spirits and make exceptions only when they are necessary.
Chandra Patni
Dec 27 '09 at 8:18
+1 for an explicit example of the response. This helped me to understand a 201 response.
Mike Moore
Oct 4 '12 at 19:59
@ChandraPatni,
Atom is dead
. Need better examples.
Pacerier
Oct 8 '15 at 10:51
Atom may be dead, but the spirit of the example is still spot on.
Ashimema
Oct 27 '15 at 6:09
show
more comments
up vote
11
down vote
Check out
HTTP: Method Definitions: POST
The action performed by the POST method might not result in a resource that can be identified by a URI. In this case, either 200 (OK) or 204 (No Content) is the appropriate response status, depending on whether or not the response includes an entity that describes the result.
If a resource has been created on the origin server, the response SHOULD be 201 (Created) and contain an entity which describes the status of the request and refers to the new resource, and a Location header (see section 14.30).
share
improve this answer
answered
May 16 '14 at 20:45
mattdipasquale
41.7k
69
291
457
add a comment
up vote
down vote
The idea is that the response body gives you a page that links you to the thing:
201 Created
The 201 (Created) status code indicates that the request has been fulfilled and has resulted in one or more new resources being created. The primary resource created by the request is identified by either a Location header field in the response or, if no Location field is received, by the effective request URI.
This means that you would include a
Location
in the response
header
that gives the URL of where you can find the newly created
thing
HTTP/1.1 201 Created
Date: Sat, 02 Apr 2016 12:22:40 GMT
Location: http://stackoverflow.com/a/36373586/12597
Response body
They then go on to mention what you should include in the response
body
The 201 response payload typically describes and links to the resource(s) created.
For the human using the browser, you give them something they can look at, and click, to get to their newly created resource:
HTTP/1.1 201 Created
Date: Sat, 02 Apr 2016 12:22:40 GMT
Location: http://stackoverflow.com/a/36373586/12597
Content-Type: text/html
Your answer has been saved!
Click here to view it.
If the page will only be used by a robot, the it makes sense to have the response be computer readable:
HTTP/1.1 201 Created
Date: Sat, 02 Apr 2016 12:22:40 GMT
Location: http://stackoverflow.com/a/36373586/12597
Content-Type: application/xml
Or, if you prefer:
HTTP/1.1 201 Created
Date: Sat, 02 Apr 2016 12:22:40 GMT
Location: http://stackoverflow.com/a/36373586/12597
Content-Type: application/json
"questionID": 1860645,
"answerID": 36373586,
"primary": "/a/36373586/12597",
"additional": [
"http://stackoverflow.com/questions/1860645/create-request-with-post-which-response-codes-200-or-201-and-content/36373586#36373586",
"http://stackoverflow.com/a/36373586/12597"
The response is entirely up to you; it's arbitrarily what you'd like.
Cache friendly
Finally there's the optimization that i can pre-cache the created resource (because i already have the content; i just uploaded it). The server can return a date or ETag which i can store with the content i just uploaded:
See
Section 7.2
for a discussion of the meaning and purpose of validator header fields, such as ETag and Last-Modified, in a 201 response.
HTTP/1.1 201 Created
Date: Sat, 02 Apr 2016 12:22:40 GMT
Location: http://stackoverflow.com/a/23704283/12597
Content-Type: text/html
ETag: JF2CA53BOMQGU5LTOQQGC3RAMV4GC3LQNRSS4
Last-Modified: Sat, 02 Apr 2016 12:22:39 GMT
Your answer has been saved!
Click here to view it.
And
ETag
s are purely arbitrary values. Having them be different when a resource changes (and caches need to be updated) is all that matters. The ETag is usually a hash (e.g. SHA2). But it can be a database
rowversion
, or an incrementing revision number. Anything that will
change
when the
thing
changes.
share
improve this answer
edited
Apr 2 at 13:15
answered
Apr 2 at 12:58
Ian Boyd
85.2k
150
529
857
So far you're response seems most sensible. I'm a little anxious about the ontology of the response, but aside from that, it seems like the most mature interpretation of the spec. I am curious if there's any sort of lightweight "responsive" way to handle human/machine output. but mostly I'm intrigued by your "caching your own input" suggestion. Most web apps I know are not going to create a 1:1 version of the resource. Even if it's something trivial like normalizing capitalization of a string. Isn't it a bit dodgy to treat your submitted version as the version the etag was created against?
Anthony
Aug 30 at 12:57
add a comment
up vote
down vote
The output is actually dependent on the content type being requested. However, at minimum you should put the resource that was created in Location. Just like the Post-Redirect-Get pattern.
In my case I leave it blank until requested otherwise. Since that is the behavior of JAX-RS when using Response.created().
However, just note that browsers and frameworks like Angular do not follow 201's automatically. I have noted the behaviour in
share
improve this answer
answered
May 3 '13 at 7:20
Archimedes Trajano
6,649
49
78
add a comment
up vote
down vote
Another answer I would have for this would be to take a pragmatic approach and keep your
REST API contract
simple. In my case I had refactored my REST API to make things more testable without resorting to JavaScript or XHR, just simple HTML forms and links.
So to be more specific on your question above, I'd just use return code
200
and have the returned message contain a JSON message that your application can understand. Depending on your needs it may require the ID of the object that is newly created so the web application can get the data in another call.
One note, in my refactored API contract, POST responses should not contain any cacheable data as POSTs are not really cachable, so limit it to IDs that can be requested and cached using a GET request.
share
improve this answer
answered
Jul 23 '14 at 14:49
Archimedes Trajano
6,649
49
78
add a comment
Not the answer you're looking for? Browse other questions tagged
rest
content-type
http-status-codes
or
ask your own question
asked
6 years ago
viewed
45921 times
active
6 months ago
Visit Chat
Linked
REST api for field validation
Related
REST: Is http code 300 appropriate in this redirect situation?
49
Correct http status code for resource which requires authorization
Http response codes for invalid data and data conflict
119
400 vs 422 response to POST of data
ReST: http 204 status code for polling for a resource after a 201 Created
Why is the HTTP location header only set for POST requests/201 (Created) responses?
HTTP response code when resource creation POST fails due to existing matching resource
what should be HTTP status code if resource is not available for requested action?
Response entity type for 201 Created?
HTTP POST response Location header when creating multiple resources
Hot Network Questions
Can Communism become a stable economic strategy? How?
Section of a book that explains things
How do I remove the remaining part of a word in the shell?
Find duplicates of a file by content
Physically locating the server
Please explain what is wrong with my proof by contradiction.
Will credit card payment from abroad be suspicious as taxable income?
Does Salesforce strictly enforce the picklist as an ENUM?
ListPlot with different color options
Is it possible to inverse selection in Object mode?
Is there a place in academia for someone who compulsively solves every problem on their own?
Draw an asterisk triangle
What is the bandwidth cost of running a full node?
Why was Gilderoy Lockhart unable to be cured?
Why is the Greek definite article τη duplicated in this sentence?
Can I stack an Animated Shield with the Shield spell?
Are backpack nets an effective deterrent when going to rougher parts of the world?
What am I? Riddle question
Where are the oil platforms in Google Earth?
Why can't alcohols form hydrogen-bonded dimers like carboxylic acids?
How do I install a turbocharger and a supercharger on a 2008 Hyundai Accent?
Asking client for discount on tickets to amusement park
Font with Dollars but no line through it
How to say “let's” in Portuguese?
more hot questions
question feed
tour
help
blog
chat
data
legal
work here
advertising info
mobile
feedback
Technology
Life / Arts
Culture / Recreation
Science
Other
Stack Overflow
Server Fault
Super User
Web Applications
Ask Ubuntu
Webmasters
Game Development
TeX - LaTeX
Programmers
Unix & Linux
Ask Different (Apple)
WordPress Development
Geographic Information Systems
Electrical Engineering
Android Enthusiasts
Information Security
Database Administrators
Drupal Answers
SharePoint
User Experience
Mathematica
Salesforce
ExpressionEngine® Answers
Cryptography
Code Review
Magento
Signal Processing
Raspberry Pi
Programming Puzzles & Code Golf
more (7)
Photography
Science Fiction & Fantasy
Graphic Design
Movies & TV
Music: Practice & Theory
Seasoned Advice (cooking)
Home Improvement
Personal Finance & Money
Academia
more (8)
English Language & Usage
Skeptics
Mi Yodeya (Judaism)
Travel
Christianity
English Language Learners
Japanese Language
Arqade (gaming)
Bicycles
Role-playing Games
Anime & Manga
more (18)
Mathematics
Cross Validated (stats)
Theoretical Computer Science
Physics
MathOverflow
Chemistry
Biology
Computer Science
Philosophy
more (3)
Stack Apps
Meta Stack Exchange
Area 51
Stack Overflow Careers
site design / logo © 2016 Stack Exchange Inc; user contributions licensed under
cc by-sa 3.0
with
attribution required
rev 2016.10.10.4051