restcookbook | Jekyll cms site for restcookbook.com | Plugin library
kandi X-RAY | restcookbook Summary
kandi X-RAY | restcookbook Summary
Jekyll cms site for restcookbook.com
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of restcookbook
restcookbook Key Features
restcookbook Examples and Code Snippets
Community Discussions
Trending Discussions on restcookbook
QUESTION
Creating a resource in an async REST API, is it valid if my server responds with an incomplete resource with a status indicator instead of returning a temporary resource?
As an example, lets say I have a service that notarizes messages adding them in a blockchain, in order to prove their existence later (like "Proof of Existence").
The execution takes a while so its a good idea to make is asynchronous.
The user sends a POST /messages
, with the payload:
ANSWER
Answered 2018-Dec-21 at 08:24According to me the method you are following is correct. The core purpose of 202 Accepted status is that 'Request is accepted and the server will process it'.
Now the only problem I can see here is Until when? To answer that I recommend that you can add some calculation which will check the average time it takes to process a similar request and send them in 1st response.
QUESTION
In REST API if I have a resource which creation could take considerable amount of time I could return a temporary resource with status code 202. Client then could poll this temporary resource until the actual resource is created and get redirected to it when it'd done (with 303 status code). Something like described in http://restcookbook.com/Resources/asynchroneous-operations/.
Is there any standardized way to created such resources in OData?
...ANSWER
Answered 2018-Dec-11 at 09:36Asynchronous requests are (briefly) mentioned in the OData V4 specification. It's probably worth reading for the fine details, but in short:
A client makes a request which includes a Prefer: respond-async
header. The server can then respond with a HTTP 202
response as you described. This response includes a Location
header which points to the 'status monitor resource'.
When the client sends a request to the status monitor resource there are 3 main responses:
HTTP 202
: The operation is not yet completed.HTTP 200
: The operation is completed. This response must also include theAsyncResult
header which holds the status code of the operation (e.g. a 200 for success, 5xx for error etc.). The body of this response contains the result of the operation.HTTP 404
:- The operation does not exist.
- The operation was canceled.
- The operation may have existed, but the client waited too long before requesting the status (May also be
HTTP 410 (Gone)
).
I don't know of any framework which implements this behavior, so you'll probably have to program it yourself.
QUESTION
When you use PUT endpoint to create resource in REST api, what should the endpoint return for subsequent calls after returning 201(created) for the first call? 403(cannot create since the resource already exist)? 200(updated to the same exact object?) if you change the status code after one call(201-> 200 or 403), isn't that a violation of idempotency? I looked everywhere but all I can find is you can use PUT to create but nowhere it said about status code change after resource creation. In short my question is that PUT is an idempotent method, but when it is used in resource creation, can it still change it's return status code from the following calls?
p.s. After first calls, it will be idempotent(constantly 403 or 200). And ideally I want to be able to tell the client that the resource is already created and you shouldn't call this again.(403) I know using POST is an alternative but as ID is already known to client at the point of creation I wanna use PUT method but want to know the proper REST way in terms of idempotency.
=================================================================== References of Using PUT endpoints for creating resources http://restcookbook.com/HTTP%20Methods/put-vs-post/ https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html
The fundamental difference between the POST and PUT requests is reflected in the different meaning of the Request-URI. The URI in a POST request identifies the resource that will handle the enclosed entity. That resource might be a data-accepting process, a gateway to some other protocol, or a separate entity that accepts annotations. In contrast, the URI in a PUT request identifies the entity enclosed with the request -- the user agent knows what URI is intended 9.6. PUT If a new resource is created, the origin server MUST inform the user agent via the 201 (Created) response. If an existing resource is modified, either the 200 (OK) or 204 (No Content) response codes SHOULD be sent to indicate successful completion of the request.
http://zalando.github.io/restful-api-guidelines/http/Http.html
...PUT requests are usually robust against non-existence of resources by implicitly creating before updating
successful PUT requests will usually generate 200 or 204 (if the resource was updated - with or without actual content returned), and 201 (if the resource was created)
ANSWER
Answered 2017-Jul-11 at 06:07Idempotency is about the server state - not about the responses. E.g. DELETE is idempotent, but after the 2nd try the resource will not be found and you may choose to respond with 404. But the state of the server is going to be the same - the resource is deleted.
Same with PUT - you can invoke it multiple times, but the state of the server will always be the same after the operation is finished.
Ideally though you could reuse PUT for updating the resources. So when the 2nd request is arrived you can use that for updating instead of returning errors. That will probably simplify implementation and the contract.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install restcookbook
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page