REST services - How to hint clients on how to represent data while maintaining decoupling

67 Views Asked by At

For simplicity's sake, let's say we are storing users' personal informations like so (JSON here, but that's not the point):

{
    "name": "John"
    "age": 35
    "sex": "M"
}

We want to have the UI client to create a form in order to display these infos and also for creating new users or updating existing ones.

So, my question is:

How can this be achieved in a RESTful manner? Does REST even provide for such type of interactions, that is, hinting clients on how to display the resources provided?

We would like to give clients maximum freedom on how to represent resources but also to help them send us back the correct data without too much coupling between backend and frontend.

For example, we could have a template for user like so:

{
     "self": "/template/user"
     "method": "GET"
     "data": {
         "fields": [
              {
                  "name": "name"
                  "value": {
                      "data_type": "string"                      
                  }
              },
              {
                  "name": "age"
                  "value": {
                      "data_type": "number"                      
                  }
              },
              {
                  "name": "sex"
                  "value": {
                      "data_type": "string"
                      "options": [
                          "M",
                          "F"
                      ]

                  }
              }
         ]
    }
}

Thank you for any input you might be able to provide.

1

There are 1 best solutions below

1
On

How can this be achieved in a RESTful manner? Does REST even provide for such type of interactions, that is, hinting clients on how to display the resources provided?

The reference application for REST is the world wide web; how did we do it there?

1) we used a media type (HTML) that supported in band presentation instructions that could, at the client's discretion, be interpreted by the client's choice of rendering engine

2) we provided links to ancillary resources (CSS) that could optionally be fetched to provide additional presentation hints to the rendering engine.

Note that this depended on text/html and text/css being well defined (standardized) so that rendering engines (browsers) could be developed independently from the servers.

I haven't seen any alternatives to HTML as a standard for sharing rendering instructions with the client. My suggestion as a first approach would be to link your data representation to an html representation, that can be used by clients that understand rendering.