2

Novice Question:

I have a rest api in my application : https://hostname/app-rest/session/manager/active-session/count. The API returns the number of sessions with certain attributes like connection type, status, etc.

When I set the request header Content-Type: application/json, response header Accept: application/json and send a request with body like: {"loggedIn": "yes", connectionType" : "mobile"}, I get a response with a number 0, 1, 2 etc.

QUESTION: Based on this: http://json.org/, it seems that the response is a valid JSON format. However, is this good design practice? Isn't it preferable to return the value in a key value pair format so that all clients can understand and parse the response - instead of assuming that the client has the knowledge of what is returned by the API ?

0

1 Answer 1

3

According to the original RFC, a number on its own is not a valid JSON text. JSON must always begin with { or [ (optionally preceded by some whitespace characters).

There is a new RFC that revises this by allowing any JSON value to be an entire JSON text. If you go by this RFC, then a number on its own is calid JSON.

For the time being I would recommend returning an object, with a key describing the meaning of the number returned, since that will conform to both RFCs.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.