1

Please let me know when to use GET method and when POST in REST API. As Post is supposed to be more secure, so can we use in case of getting the data from the database in REST API or it just can be used in case of adding new record in the database. Thanks for your views.

user2267023
  • 37
  • 2
  • 8
  • 1
    One is not more secure than the other. – Luciano van der Veekens Sep 04 '17 at 12:29
  • The only thing more secure about POST is that the request payload is usually not logged by the server. – Björn Tantau Sep 04 '17 at 12:29
  • In brief Use GET for safe and idempotent requests Use POST for neither safe nor idempotent requests – Master Yoda Sep 04 '17 at 12:32
  • also a lot of XSRF prevention is done for POST/PUT only assuming the GET operation is not updating any data. How secure the API is, is down to how the developer secures it not that POST is inherently more secure than GET unless there is something I've missed – MattjeS Sep 04 '17 at 12:37

2 Answers2

5

Usecases for each method:

  • GET: "Hi, Mr. Server. Could you please recover some information for me?"
  • POST: "Hi, Mr. Server. Could you please store this information for me?"
  • PUT: "Hi, Mr. Server. Could please update the previous info you stored for me?
  • DELETE: "Hi, Mr. Server. I think I don't need anymore this information. Could you please delete it for me?

There are some other methods but those are the most common ones

Alberto S.
  • 7,409
  • 6
  • 27
  • 46
  • Unless the query is **very** complex you should use a `GET`. By a "complex query" I mean really complex querys that need, for example, a JSON with nested elements describing them. For regular queries query parameters whould be enough – Alberto S. Sep 04 '17 at 12:39
0

The following thread has a nice answer regarding this: https://stackoverflow.com/a/37331668/8558426

TL;DR: Which method should you use?

It depends on the operation you are performing. In REST APIs, the POST method is frequently used to create resources while the GET method is frequently used to request a representation of a resource.

Luke Glazebrook
  • 580
  • 2
  • 14