-3

I had an argument with my PHP developer today who tried to convince me that REST API has to provide all the request from PHP since a server is faster than client devices such as desktop.

As I am a REST API developer, I told him that it would be better if client can reduce the number of API request since since client devices can handle more stuff these days with powerful CPU power and greater memory. This is what we are trying to do. The REST API will provide a list of sale items for e-commerce site with a category of each items.

I was asking PHP developer to generate filtering option based on provided categories in the item list. The PHP developer asked me to to provide the filtering option as a JSON.

He wants to request two for this transaction, but I told him he can do it by only one request. Which way is right?

2
  • 2
    What does "filtering option as a JSON mean?" He wants you to provide the categories in JSON? That's not an unreasonable request. But why does your PHP developer think two REST requests are required, if this is a single business transaction? The reason might be relevant; merely saying "less requests are always better" is not a convincing argument. Commented Jul 21, 2015 at 6:02
  • 3
    I am not able to comprehend this question at all. Could you reword it to give better background info and what the two options include? Commented Jul 21, 2015 at 7:32

1 Answer 1

2

If I'm understanding you correctly, you are providing a list of items with 2 (or more) categories and you want to display them sorted or separated in some way.

The options are multiple requests, one for each selected category. Or a single request allowing the client to specify multiple categories.

If that's in any way close to the correct understanding, this is how I would look at it...

Primarily, right or wrong, seems to be a harsh way to look at it. Advantages and disadvantages might be a less confrontational way to approach it and it might make it easier to come to an agreement.

The advantages of multiple requests:

  • API simplicity, Requesting a list of Categories in a single request would add (presumably a small amount) of complexity to the API which may be unnecessary if you don't expect complex requests
  • Interface simplicity, No filtering code in the interface.
  • Fairly large records (lots of data, compared to the overhead of the connection) in a situation where multi category requests are the exception would be one situation where this would work.

Advantages of a single request:

  • There is a non-trivial amount of overhead to the additional connections. This is compounded by things like cookies which on some sites can add up to a significant percentage of the request/response bandwidth for small images and ajax requests.
  • An environment where multiple category requests are the norm especially if the records (and number of results) are small would favor this approach.

In general, I would say less requests is absolutely what you would want to aim for. But you really need to evaluate your individual situation and take into account a wider picture of the system and how it is likely to be used. And this may change over time. A system designed with a total of 3 categories, that grows over time to hundreds of categories would probably need to be reevaluated.

2
  • 1
    This answer is exactly what I was looking for as a REST API beginner. Thank you so much. I am still building it with Ruby/Grape and your answer gives me a better understating of how the data has to be presented.
    – Hoorayo
    Commented Oct 23, 2015 at 17:19
  • Glad I could help! Commented Oct 23, 2015 at 18:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.