Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convention for combining REST requests and replies?

Tags:

rest

For a complex web application, one page view might require data of many different types. If you are doing client-side templating and pulling in this data from a RESTful web service, you may have to make a lot of requests to populate the page. Is there any convention by which REST requests can be combined, to receive back a single payload containing all the responses needed?

To use the Stack Exchange User page as an example, you probably would need to call at least these from the Stack Exchange API to build the page:

  • /users/{id}
  • /users/{id}/answers
  • /users/{id}/reputation
  • /users/{id}/tags
  • /users/{id}/questions
  • ...etc

Could the provider of such an API allow you a single request to get all that data? Especially if other root types were involved, say you had to also request /revisions/{id} and /tags to build the page also for some reason.

To be clear, I don't want to know if it's possible to provide such functionality in a web API -- I want to know if there is a standard or at least documented means of doing so.

like image 244
jwl Avatar asked Jan 31 '13 22:01

jwl


People also ask

What are REST API conventions?

When you provide JSON data as input for the create or update operations, the REST API takes into account only the properties that are writable on the element. The API ignores all other data. Read-only properties, such as computed counts or creation dates, are not updated. The API ignores unrecognized properties.

How do you handle multiple REST API calls?

The REST API can handle multiple client requests by using the following methods: Using the asynchronous nature of HTTP: The client can make multiple requests to the REST API, and the REST API can handle those requests in an asynchronous manner.

Can API handle multiple requests?

If you need to make multiple API requests, you can send these API requests concurrently instead of sending them one by one. Sometimes, we need to make multiple API calls at once. For example, let's say we have an array, and we want to make an API request for each element of that array.


2 Answers

I think we need some standard way of doing this, but until then, you're probably on your own.

In the past I've had similar challenges when I wanted to do transactions spread across multiple resources. I invented new resources which described the actions I was about to make more clearly (subtracting 5 from account A and adding 5 to account B becomes a transaction with from and to members).

Maybe a similar (but more general) approach could be applied here by using another REST call as container;

POST /batchRequests { requests: [
    { method: 'POST', url: '/resource', payload: { ... } }
], transactional: false }

Which in turn returned a "BatchRequest" object containing results. It's pretty close to what Facebook did as well.

like image 59
Thomas Jensen Avatar answered Dec 27 '22 05:12

Thomas Jensen


I think that might be against REST principles, no? i.e. the unique URI per object representation and its payload. Wouldn't it make things worse? Given that it's not TI calculator but a modern computer capable of parallel HTTP requests verses a single one that'll take as long as the logest of them all.

Don't think there is a standard, but here's an example -- https://developers.facebook.com/docs/reference/api/batch/

like image 40
Ilya Kozhevnikov Avatar answered Dec 27 '22 04:12

Ilya Kozhevnikov