0

I'm using mongoDB, mongoose and express, and I want to use the "find" method like this:

checklist.find( { _id: { $in: checklistIdsArray } } )

Basically I want to find all checklists that are listed in the checklistIdsArray. Now this works fine, but I used a POST request instead of a GET request, because I needed to send the "checklistIdsArray" object to the server and I cannot do it in a GET request, as the GET request doesn't have a body, well technically it can have but it is not recommended.

function getTripChecklists(checklistsIds) {
            var URL = SERVER_URL + '/checklists/tripchecklists';
            return $http.post(URL, checklistsIds)
                .then(successHandler)
                .catch(errorHandler);
}

Is this the best approach to accomplish what I want, or is there a better way ?

FraK
  • 690
  • 10
  • 18
  • the `/checklists/tripchecklists` endpoint doesn't accept query parameters? – matias elgart Nov 14 '16 at 20:35
  • it can, but the checklistIdsArray can be quite large with elements like these: ["582a1174affead1b0c0fbb55". "582a1174affead1b0c0fbb55". etc] I cannot put those as params, too large. – FraK Nov 14 '16 at 20:47
  • In my suggestion,In terms of performance this is a best approach. – Simran Nov 16 '16 at 11:04

2 Answers2

1

one RESTful way to do this would be to have an endpoint that creates a 'query results resource' via a POST (with ids in the body) that you can then fetch with a GET afterwards. this way is especially useful if your checklistIds is a long list of ids or if the processing of gathering the query results might take a while.

another simpler way is to pass checklistIds as a query parameter, useful if you know the number of ids will be small.

there's a larger discussion about this in another SO answer: HTTP GET with request body

Community
  • 1
  • 1
matias elgart
  • 965
  • 9
  • 17
  • I cannot pass the ids as they are large and don't know the exact number of them. Can you explain in more detail your first idea about having an endpoint with POST? wouldn't it be the same as just having the POST request but also adding a GET request inside the server ? – FraK Nov 15 '16 at 16:41
  • sure. i meant two related but different thing. first, it's not uncommon to use a POST if your expected payload would be too large. in this case, POST with the payload in the POST body, fetch your trip checklists and return in the POST response. the second idea is that if your query might actually take a while to process, you could POST your query which instead of blocking would return a 'queryId' some other way for you to come back later with a GET and fetch query results. really depends on your resource usage. hope this helps clarify. – matias elgart Nov 15 '16 at 21:05
  • How is it possible to come back later and fetch the query results with the queryID ? I didn't know you could do that ?! – FraK Nov 17 '16 at 22:49
1

Well, when you talk about 'best' you would need to stick to convention and RESTful convention doesn't suggest to use POST to fetch data, it should be GET always.

We are already ignoring the query param because it's tedious and isn't favored. So yes POST can be used, it won't be RESTful but then instead of multiple call, only one call would suffice and you are winning in terms of performance.

If you want to stick to RESTful, you might want to see the scope of designing it differently, like what if you call all the checklist beforehand and use as per the chosen checklist on the UI? what if the API which provided checklistIds can provide the checklist appended to it, so that you don't need a separate call.

Ofcourse you would be thinking of redundancy and large response but then we have to define, what we call 'best'.

Rahul Kumar
  • 2,343
  • 1
  • 15
  • 25