Responses

From MSX - Wiki
Revision as of 15:56, 8 January 2021 by Benzac (talk | contribs) (Created page with "By default, the JSON data is provided directly as root object. Please see this example code of a menu root object. <syntaxhighlight lang="json"> { "headline": "My Menu",...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

By default, the JSON data is provided directly as root object. Please see this example code of a menu root object.

{
    "headline": "My Menu",
    "menu": [{
            "label": "My Menu Item"
        }]
}

This data structure is very suitable if the HTTP server hosts static JSON files. However, if the HTTP server uses a database or an external API to dynamically create JSON files, an extended data structure is useful. Therefore, it is possible to wrap any JSON data in a response object to have an unified root object and to provide advanced error handling. Please see this example code of a wrapped menu root object.

{
    "response": {
        "status": 200,
        "text": "OK",
        "message": null,
        "data": {
            "headline": "My Menu",
            "menu": [{
                    "label": "My Menu Item"
                }]
        }
    }
}

The next example code shows how an error response can look like.

{
    "response": {
        "status": 404,
        "text": "Not Found",
        "message": "Menu for database ID 'xyz' not found.",
        "data": null
    }
}

If you execute actions on server side, the server action response must always be wrapped in a response object. Please see the next example code of how a server action response can look like.

{
    "response": {
        "status": 200,
        "text": "OK",
        "message": null,
        "data": {
            "action": "menu:data",
            "data": {
                "headline": "My Menu",
                "menu": [{
                        "label": "My Menu Item"
                    }]
            }
        }
    }
}

Please see Server Actions for more information.