2

I want to retrieve data about a bunch of resources. Let's say an Array of book id and the response is JSON Array of book objects. I want to send the request payload as JSON to the server.

Should I use GET and POST method?

Note: I don't want to make multiple GET request for each book ID.

POST seems to be confusing as it is supposed to be used only when the request creates a resource or modifies the server state.

Sivasankar
  • 753
  • 8
  • 22
  • 1
    Strictly speaking, you should use GET, and pass in the collection of book ids which you want to retrieve. But, if that book list might exceed what can fit in the query string of a URL, then you might have to use POST anyway. – Tim Biegeleisen Jul 02 '19 at 05:18
  • Update: Guess, I am not looking for a code solution. I wanna find out what's the best to use? `GET` or `POST` as per REST API definition. – Sivasankar Jul 02 '19 at 10:57
  • @Siva what was the approach you used at the end? I am in a similar situation as you were when asking this question. – mlst Apr 04 '21 at 16:10

3 Answers3

2

I want to retrieve data about a bunch of resources. Let's say an Array of book id and the response is JSON Array of book objects.

If you are thinking about passing the array of book id as the message body of the HTTP Request, then GET is a bad idea.

A payload within a GET request message has no defined semantics; sending a payload body on a GET request might cause some existing implementations to reject the request.

You should use POST instead

POST seems to be confusing as it is supposed to be used only when the request creates a resource or modifies the server state.

That's not quite right. POST can be used for anything -- see GraphQL or SOAP. But what you give up by using POST is the ability of intermediate components to participate in the conversation.

For example, for cases that are effectively read-only, you would like to use a safe method, because that allows pre-caching optimization, and automated retry of lost responses on an unreliable network. POST doesn't have extra semantic constraints, so you lose out.

What HTTP really wants is that you GET using the URI; this can be done in one of two relatively straightforward ways:

  1. POST the ids to the server, to create a new resource (meaning that the server retains for itself a copy of the list of ids), and receive a new resource identifier back in exchange. Then GET using this new identifier any time you want to know the current representation of the results.

  2. Encode the information you need into the URI itself. Most commonly, this is done using the query part of the URI, although that isn't strictly necessary. The downside here is that if the URI encoded representation of the array of ids is very long, you may have trouble with some implementations that enforce arbitrary URI limits.

There aren't always great answers:

The REST interface is designed to be efficient for large-grain hypermedia data transfer, optimizing for the common case of the Web, but resulting in an interface that is not optimal for other forms of architectural interaction.

Community
  • 1
  • 1
VoiceOfUnreason
  • 52,766
  • 5
  • 49
  • 91
0

If I understand correctly, you want to get a list of all of the items in a list, in one pull. This would be possible using GET, as REST returns the JSON it can by default be up to 100 items, and you can get more items if needed by specifying $top.

As far as writing back or to the server, POST would be what your looking for, this to my understanding would need to be one for one.

K Henson
  • 11
  • 2
0

you are going to use a GET-Request and put your request-data (book-id array) in the data-section of your ajax (or whatever you're going to use) request. See How to pass parameters in GET requests with jQuery

Hero1587
  • 68
  • 5