3

I'm currently working on REST API. I like the idea of GraphQL or OData to allow developer to choose which columns/fields to return and relation record. But GraphQL require migration and oData have too long of query string. I'm thinking is it possible to use http-GET method to pass in json body to archive that. For example, use http-Get for customers to retrieve customer name 'Luke Skywalker'

{  
   "customers":{  
      "Id":null,
      "name":"Luke Skywalker",
      "height":null,
      "DateOfBirth":null,
      "Cars":{  
         "manufacturer":"BMW",
         "model":null,
         "Plate":null
      }
   }
}

And below to get same customer name but on invoice information

{  
   "customers":{  
      "Id":null,
      "name":"Luke Skywalker",
      "height":null,
      "DateOfBirth":null,
      "Invoices":{  
         "Year":2019,
         "Outstanding":Yes,
         "OutstandingAmount":null
      }
   }
}  

Any comment on this method?

Brhaka
  • 1,622
  • 3
  • 11
  • 31
Tong
  • 31
  • 1
  • 2
  • `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.` Source: [RFC 7231](https://tools.ietf.org/html/rfc7231#section-4.3.1) – Roman Vottner Jul 24 '19 at 10:31
  • In addition to that, GraphQL is just RPC. It has hardly anything to do with what REST tries to acoomplish - the decoupling of clients from servers. In REST clients are servered options clients can chose upon to progress their task. This does not require internal knowledge of the API as everything needed is given by the API. GraphQL on the other end requires some internal knowledge of the available fields frist and therefore tightly couples to the API. This is why GraphQL does not go well with REST! – Roman Vottner Jul 24 '19 at 10:35
  • My REST API is aim for my own application(s) only, so "sending a payload body on a GET request might cause some existing implementations to reject the request." doesn't apply on my case. PostMan also recently started to allow GET in the body for testing. https://stackoverflow.com/questions/978061/http-get-with-request-body If not using HTTP GET method, what about 'Patch' to achieve GraphQL or OData alike feature? – Tong Jul 25 '19 at 01:36

1 Answers1

3

I'm thinking is it possible to use http-GET method to pass in json body to archive that.

It is against the rules.

RFC 7231 says

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.

Generic HTTP compliant components are not guaranteed to understand what you are trying to do with the request.

Fundamentally, HTTP is based on the idea that the URI of a resource serves as a cache key, that can be used to optimize the retrieval of resources and reduce network load. There are no contingencies available for the case where the URI and the request body together should serve as the key.

REST depends on semantics that are shared across many implementations; in HTTP, the many implementations have agreed upon the uniform interface defined by the specification. When you try to change that interface, unilaterally, you lose the benefits that it provides.

If you can't communicate the parameters you need via the URI (for instance, by encoding the JSON body into the request part), then the right thing to do is fall back on POST, which is semantically correct but alas doesn't communicate to generic components the read only semantics you would prefer that they understand.

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