What does code 201 of a Request mean?

Asked

Viewed 1,657 times

5

In research I saw that the code 201 of a Request means :

The request was fulfilled and resulted in a new resource being created.

But I didn’t really understand the meaning of it.

What would be an example to better understand this statement ?

  • See help: http://stackoverflow.com/questions/1860645/create-request-with-post-which-response-codes-200-or-201-and-content

1 answer

8


In a Rest API it is common to create new features. For example, you post to:

http://meudominio.com/minhaApi/meuRecurso

This creates a new feature that can be accessed at:

http://meudominio.com/minhaApi/meuRecurso/1234

Where 1234 is the id of the resource.

Many Apis make the decision to return one 200 OK and the body of the resource created in the request, but that doesn’t always make sense... If someone just created the resource he probably has the information on his side (or at least everything that wasn’t generated on the server side, like the id of the application in the above example). On the other hand returning the complete information can be good practice.

Another option is to exactly return the code 201 Created with a link to the new resource:

HTTP/1.1 201 Created
Date: Wed, 07 Dez 2016 15:01:27 GMT
Location: http://meudominio.com/minhaApi/meuRecurso/1234

Basically this is a way to combine an http response with Hypermedia (link) to tell a user that the feature has been created and that he can retrieve it at the address in question. Regardless of whether to return the body of the object or not, the code 201 has a more specific meaning than 200, basically the server not only understood the request as it created or is creating something in response to it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.