Is it good that the REST api is exposed to two HTTP methods?

The problem is that we have a complex query string for finding api and you want users to have usability of the body. Therefore, we want to enable both GET and POST (or PUT).

I understand that there will be debate that the search is a read-only operation, and it should be installed only in accordance with REST standards. I also understand that PUT does not support cache. But I also know that from time to time it deviates from REST standards. But does it make sense to have two convenience methods for customers?

+3
source share
2 answers

Using POST directly to query data is not very good, precisely for the reasons you talked about. If your search string is complex, perhaps you can simplify it by dividing the request process in two steps - one with POST and one with direct GET s.

The first step is to create a request template using POST . The request string is sent through the body of the message and becomes a new resource that users can request through GET . The query string allows you to use parameters similar to SQL queries. It's amazing what your query looks like, here is an example:

 (userName = $name) || (createdBefore > $asOf && deleted=false) 

Your users will POST this in the body of the message and return a new resource identifier. This resource identifies the parameterized "view" in your data. Let say that the resource identifier for this view is aabb02kjh . Now your users can request it as follows:

 https://app.yourserver.net/aabb02kjh?name=airboss&asof=20140101 

This adds some complexity to your API, but allows users to define and reuse query templates with very simple and standard query strings.

+3
source

Interest Ask. I mean POST -> PUT, DELETE. There are general ways to bypass HTTP methods:

  • sending a hidden _method input _method with message data
  • sending _method request _method to URL
  • sending an X-HTTP-Method-Override header with a message

etc. So, if they are valid (I'm not sure about this), then you can also use the same GET approach.

According to the restrictions of REST: the cache and the unified interface, as well as the definition of the HTTP method, you must use GET for search queries. There are only a few URL query languages ​​to read URLs, such as RQL, but you can always select your favorite query language and serialize it to use URLs ...

Another interesting approach is to add link descriptions about the url. (But this is very new to me.)

+1
source

Source: https://habr.com/ru/post/1202170/


All Articles