Search code examples
resthttpserverclienttightly-coupled-code

Should HTTP REST request to the Server return data in specific format expected by the Client?


If using a library on the client-side that expects data in a specific format (e.g. [{id: 1, name: "Jack", available: true}]), should the server process the data in the exact structure requested by the client or send back a generic data (e.g. [{userId: 1, username: "Jack", isUserAvailable: true}]) which can then be modeled on the client-side to avoid tight coupling and breaking if the client-side library changes in the future?


Solution

  • Client have to depend towards the interface that the server provides, This interface can be seen as a contract that the server and client agree to. This does imply the data structures provided and is a form of coupling. Hence the need to clearly describe / define APIs and have a policy for versioning and obsoleting those.

    So this may seem like a tight coupling at first glance, but does not have to be. Client and server may or may not use the same language / representation of the data. The client is free to do whatever it wants the with the JSON in this example. It may use all the data or just a single attribute. All of that is of no concern to the server. Similarly the client is not concerned with how the server has created this JSON string. Due to the service contract only describing the interface and the resulting freedom for server / client implementations the coupling can be considered loose (enough).