2

I am building out a REST API and was wondering if there is any disadvantage or recommendation against making multiple requests or one request that returns an array of objects. I searched around and wasn't able to find discussions about this, so I apologize if it is out there.

For example:

Multiple Requests:

GET#1

[ { "colors": ['blue','red','green'] } ]

GET#2

[ { "animals": ['dog','cat','bird'] } ]

GET#3

[ { "names": ['John','Jacob','Josh'] } ]

Or a single request:

GET#1

[ 
  { 
    "colors": ['blue','red','green'],
    "animals": ['dog','cat','bird'],
    "names": ['John','Jacob','Josh'] 
  } 
]

Personally, I don't mind either way. I also think that there would not be any future issues if using dictionaries. I am more so curious to know if this is looked down upon, indifferent, or even recommended.

To further clarify, the data will be related. I was looking for both general opinion as well as technical details, i.e. one request can be x time faster than trying three requests.

  • both are good, example: http://myapi/users (get all users) and http://myapi/users/2545 (get a single user) of course you need separate by groups... you are mixing all at get#1 –  Jan 31 '17 at 16:23
  • 1
    This would depend completely on the context of the query. If the objects are related to each other, there seems to be no reason to separate things. Also requests can be computationally expensive, so it's rational to try and keep their number as small as makes sense – Pekka Jan 31 '17 at 16:24

1 Answers1

2

From a performance perspective, using one GET is considerably faster. But I think it really depends on your use-case. You also want to balance performance with code readability. It's probably a good idea to group your GET calls into logical groups that make sense, and will be understandable for developers who maintain your code in the future.

Matt Spinks
  • 6,380
  • 3
  • 28
  • 47
  • 1
    Thank you, makes sense. Testing it out on a browser works fine either way, but you're right about it being faster with a single request. It is somewhat noticeable on a mobile browser. Over time, there will be added data so I think I will keep them as one request. All of the data is related. Thanks again. – John-Paul Ensign Feb 01 '17 at 18:53