Handling a RESTful presentation structure difference between POST and GET

I am developing a REST API and although you are spending a lot of best practice guides, I cannot find much regarding the best practice of handling the mismatch in the presentation structure needed for POST vs the same presentation structure returned from GET .

GET for a dummy user view might look like this:

 { "id": 1234, "created": "2012-04-23T18:25:43.511Z", "username": " johndoe@example.com ", "name": "John Doe" } 

However, the POST for the same dummy user view cannot specify specific properties (namely id and created ):

 { "username": " johndoe@example.com ", "name": "John Doe" } 

Obviously, this is an oversimplified example, but provided that the user cannot specify certain fields (and it may not always be clear which of them relate to the method used), is it best practice to create separate views for each or to expect the most complete version and transparently handle the imbalance of data on the server?

Despite the apparent ease of having a single view and handling of the server side of the mismatch, I fear that it will be a bad experience for the user if it is not clear which values ​​can be specified (or changed using PUT for example). If the trend is to create separate views, is there a naming convention to define the view?

eg. i_user for the incoming user and o_user for the outgoing user. Or user_full and user_min or user and .user etc.

Update: My overly simplified example may have incorrectly illustrated the problem. Imagine a view that has 50 properties (for example, a server view with all its monitoring attributes - cpu, ram, temp, storage_drive_a, storage_drive_b, file_permission, etc.). Of these 50 properties, 30 are read-only properties, and 20 of them are values ​​that can be set.

+5
source share
1 answer

First of all, the final semantics of the POST method are determined by the target resource, not the HTTP protocol, as with other methods, so your POST method can do whatever you want as long as you document it correctly and you don’t copy the functionality, already standardized by other methods.

So, in a word, there is nothing wrong with the fact that I have a different view for the POST and GET methods.

However, asking for best practice in this case is pointless because the media type used is used to determine the presentation format, not the method, but most of the so-called REST APIs on the Internet use the common media -types for everything, and clients rely on URI semantics to know which resource they are dealing with, which is not RESTful at all. Basically, you are asking for best practice for a problem that does not actually exist in REST when everything is done correctly.

So, in order to answer your question, you may have different views with different types of multimedia - for example, your full user view may have a media type application/vnd.mycompany.user.full.v1+json , and a simplified user view may have media type application/vnd.mycompany.user.min.v1+json - or you can have one representation of type application/vnd.mycompany.user.v1+json , and your documentation for this type of media can indicate how some may exist properties or not, or may have default values ​​if they are not specified. Your POST method will require the use of one type of media and will respond with 415 Unsupported Media Type if clients send anything else in the Content-Type header. Similarly, the client can select the desired view with the title Accept .

As you can see, what you are asking is not a problem when you are actually doing REST, and not just using it as a buzzword for the HTTP API.

+2
source

All Articles