HATEOAS REST API – Ideal Way to Send Success Messages

hateoasrest

I am using HATEOAS architecture in my rest application and want to send internationalized success message that will be directly consumed by the client. I know we can just add a key in the response but want to know what is the ideal way architecturally.

For example, lets say i want to send a message Department created successfully in my response below. How would i go about that?

Uri: http://api.domain.com/management/departments
Type: POST

Existing response:

{
    "departmentId": 10,
    "departmentName": "Administration",
    "locationId": 1700,
    "managerId": 200,
    "links": [
        {
            "href": "10/employees",
            "rel": "employees",
            "type" : "GET"
        }
    ]
}

Best Answer

Remember, you are not sending messages from the server to the clients. You are sharing resources. Representations of resources you have somewhere. More specifically, their status. A projection of them. The client is "asking" for something you have in your server and the server is sharing a temporal representation of it.

The fact that the server did response (200, 201, etc) and the client got a representation is enough confirmation (message) for you. So if the client application handles a 20x, you can show the message you want. The server doesn't care whether the client got the response or not.

Regarding the result of the "communication" process, leave it to the protocol and its semantics. Use HTTP status code, headers and response messages according to the request methods, headers, etc.

Why?

Because the main consumers of the HTTP requests and responses are an HTTP client and an HTTP server that will react and behave according to these things. You get the most out of HATEOAS, If you don't introduce noise in the communication process and respect the protocol semantics.