0

Background

Let's assume i have some data as follows that i collect based on some survey users fill out in a form:

{
  "brand": "Nike",
  "size": [
    "small",
    "medium"
  ]
}

Now let's say I want to pass this data to some api , in my case i will create api gateway that will forward this request to a aws lambda function. The lambda function request will process this request and look at my rds instance to get all the shirts that are small and medium and return me data as response.

My question:

When I make the ajax call to the api would this be a get request or a post request??

Technically I am not really going to modify the database but rather do a read on the database something like select * from nike where size = 'small' and size = 'medium'

I am confused because i assumed whenever we are trying to "GET"/read some data we do a GET request. However I came across THIS stackoverflow post. where the accepted answer suggests that when we are passing data long like i am above we should rather make a POST request.

So my ajax call would look something like this possibly:

var data =  {
      "brand": "Nike",
      "size": [
        "small",
        "medium"
      ]
    }


$.ajax({
  type: "POST",
  url: "apigatewayendpointblahblah",
  data: data,
  success: success,
  dataType: dataType
});
Dinero
  • 767
  • 6
  • 24
  • There is nothing confusing, the names themselves guide you - if you want to *receive* data, then GET it, if you want to *send* data then POST it. The HTTP verbs are not created with complexity in mind but to be self-descriptive. The only minor confusion is that POST is often serving double (or otherwise multiple) duty for other operations instead of using PUT or DELETE. – VLAZ Feb 16 '20 at 14:45
  • @VLAZ right so i originally thought the same that i should use get but the confusing part is how do i pass a body of data in a get request ? would love it if you can show simple example – Dinero Feb 16 '20 at 14:51
  • You *don't* pass a body part of a GET request. You cannot by protocol standards. You only read data by issuing a bodyless request for a resource. Hence a GET - you are *getting* data. You can use a query string to specify stuff *about* the data, for example `GET myapp/user?id=42` will fetch you a single user while `GET myapp/user` might get a list of all users. Or you could have `GET myapp/user?type=xml` to ask for XML format and `GET myapp/user?type=json` to request it be in JSON format. – VLAZ Feb 16 '20 at 14:58
  • @VLAZ fair point so in my case would my request look something like this www.blahblah.com/shirts?brand=nike&size=small,medium? – Dinero Feb 16 '20 at 15:00

1 Answers1

2

The Rest Protocol has strict definitions for the methods you should use. when you're fetching data without modifying it, you should use GET request.

Notice that GET required query params rather than body params, which then can be used as a link for sharing pages.

Refer to https://restfulapi.net/http-methods/ for more info

fadeys.work
  • 469
  • 4
  • 12
  • So if i am using GET, which i thought originally i would use how would i pass my data along? Can i still pass it in a body? A small example would help – Dinero Feb 16 '20 at 14:50
  • 2
    use [query parameters](https://www.moesif.com/blog/technical/api-design/REST-API-Design-Best-Practices-for-Parameters-and-Query-String-Usage/) – Jason Feb 16 '20 at 14:52
  • @Jason would it look something like www.blahblah.com/shirts?brand=nike&size=small,medium? – Dinero Feb 16 '20 at 14:57
  • correct. Or could be `size=small&size=medium&size=large` to denote a list value` It really depends on the API you're consuming. Typically, both should work, but sometimes one or the other is locked down – Jason Feb 16 '20 at 15:02
  • @Jason would love to give you credit if you can post as an answer – Dinero Feb 16 '20 at 15:29
  • 1
    thank you very much :-) I really do appreciate that sentiment, but points aren't the reason I'm around here. Keep asking good questions! – Jason Feb 16 '20 at 15:35
  • @Jason so something like this would be reasonable. www.shop.com/shirts?brand=nike&size=medium&size=large&size=small I just fear if the list gets too long if there is better way to pass this information. Also this question maybe beyond this post but since i am going to forward request to lambda i guess i would be able to parse the query doing something like event.brand ? – Dinero Feb 16 '20 at 16:09
  • Dont worry about the length of query params, there's no limit to it. you can look at google's results links and see how long their query params are. So, no its not getting too long, that's the appropriate way. – fadeys.work Feb 17 '20 at 09:09