Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REST Standards - Should output models always match input models?

So I have the requirement that part of the output models must include UI important information. This information is essentially text translations and suggested formats for dates, prices, lengths.

So an example of an output model could be:

{
  statuses : {
    enumValue1 : "Display This Text",
    enumValue2 : "Display This Text2",
  },
  thePrice : {
    value : 3.50,
    formattedValue : "$3.50"
  },
  length : {
    meters 3,
    formattedValue : "3 ft."
  },
  iAmAPropertyOnlyInGet : 42
}

Now if I have this as my output model, is it "ok" to have a completely different input model?

{
  status : {
    enumValue1,
    enumValue2,
  },
  thePrice : 3.50,
  lengthInMeters : 3  
}
like image 278
BradLaney Avatar asked Nov 06 '12 17:11

BradLaney


People also ask

What makes a good RESTful API?

Good REST APIs: are well-documented and reliable. use HTTP verbs as Fielding originally defined. support X-HTTP-METHOD-Override to accommodate picky proxies.

What is model in REST API?

A request body is usually a JSON document, and the structure of that JSON document can be defined in a Model. The structure of a request body is not currently validated by the IBM Integration Bus run time. However, the Model definition can be used with a Mapping node to graphically implement the REST API operation.

What should REST API return?

The API should always return sensible HTTP status codes. API errors typically break down into 2 types: 400 series status codes for client issues & 500 series status codes for server issues. At a minimum, the API should standardize that all 400 series errors come with consumable JSON error representation.


1 Answers

Representations you send to the origin server can be completely different than the representations you receive. Consider the way web browsers work. You GET text/html and you POST application/x-www-urlencoded-form.

When using the PUT method it is normal for what you PUT and what GET to be similar if not identical.

The REST architectural style puts no constraints on the shape of the HTTP payloads, other than the fact that the semantics must be explicitly specified in the message.

So, in fact, sharing a model type between a client and server without explicitly identifying that type in the message is a violation of the self-descriptive REST sub-constraint.

like image 188
Darrel Miller Avatar answered Nov 15 '22 18:11

Darrel Miller