简体   繁体   中英

PUT bulked request body

I am wondering what is the correct approach to do this:

I want to update a bunch of items in a list and send them to the server all at once, not like PUT with an id. This is what I want:

request body: [{ oneKey: 'One value', anotherKey: 'Another value' }, { oneKey: 'One value' }]

And I want to send this down with PUT to this service: mydomain/myservice And not to anything like this mydomain/myservice/1

  • What is the correct form of the response value?
  • Should I receive the new updated values if everything goes OK?
  • Should I receive the new updated values and the other values that were not updated if everything goes OK?
  • Should I receive the old values if something goes wrong?

Or something else?

I think your questions point out the friction caused by not following a RESTful style, in a RESTful API. Generally you would send one PUT request per resource and get the appropriate response for the action, for that specific resource. But there are times that doesn't always turn out to be the most optimal way to do things. So, I've seen this solved in two different ways.

Option 1 For these types of batch scenarios you would have a separate RPC service to handles these requests. This is a totally separate process, usually a SOAP web service, or if you want to move to a newer way to do it you can take a look at grcp.io . Very effective and works great for this type of request. But, since you have another process to deal with, there's more overhead.

Option 2 Use your current RESTfull API and use a "_verb" segment in the URI. This represents an execution rather than a resource. The underscore is a visual cue for knowing it's not a resource. For example : POST https://api.mydomain.com/sales/orders/_batchUpload

The response would be whatever makes sense in your scenario. One way I've seen it done is the response object contains a list of resource Ids with a status flag for each. So, the status was "Success" or "Fail".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM