Defining a forecast for a REST GET request

Does the forecast for a REST GET request indicate a violation of the REST principle and / or is it good practice?
Consider api as /person?fields=fname,lname, address , this may be because the person is a large model, and for my current requirement I only need the value of these fields (for example, I create a user interface grid)

+6
source share
1 answer

It is quite normal to define a new resource if the current ones do not support what you want. This is how REST works.

So, in your case, the definition of URI /person?fields=fname,lname, address absolutely correct.

Note that the structure of the URI does not matter; you need to provide references to clients where you describe the URI pattern and variables. So you should return a link something like this (fictional JSON hyperlink format):

 { "_links": { "/meta/person/list": { "href": "/person{?fields}", "vars": { "fields": { "required": false, "composition": [ "fname": { "meta": "/meta/person/fname" }, "lname": { "meta": "/meta/person/lname" }, "address": { "meta": "/meta/person/address", "alternatives": { "href": "/locations", "meta": "/meta/locations" } } ] } } } } } 

In the case where /meta describes the type and label of each parameter:

Get / meta / person / fname

 { "type": "string", "label": "First name", "_links": { "self": { "href": "/meta/person/fname" } } } 

Ofc. your first step with the client is to get the whole meta, or at least the most frequently used parts. Having processed the link, the client should be able to understand the meta description and this special format only for JSON hypermedia. The structure of the URI is completely irrelevant, it should only use meta to understand what a link is and how to use it.

Unfortunately, we currently do not have a standard on how to describe links in a JSON response. There are hypermedia formats such as Hydra + Json-LD , HAL , HyperSchema , etc. But as far as I know. none of them are standard. (The Hydra RDF wokaba is probably the closest to one, but it is certainly not ready for production. Json-LD is already the standard way to represent RDF.)

Now, if your client is hard-coded to create a URI /person?fields=fname,lname,address and what this means, then this is not a REST client, because this type of service / client violates the uniform / HATEOAS REST restriction. Ofc. currently ppl. call everything as REST, RESTful, API, even when they know only as much as John Snow on this topic. Btw. there is no tragedy, if you do not want to deploy your web application in REST mode, it depends on your requirements. For example, if your application does not have a large number of users and third-party developers, it most likely does not matter which path you choose.

+3
source

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


All Articles