Alok
Alok

Reputation: 184

Need clarification on Rest API response design

We have an REST API that need to be consume from our end. Lets says

URL: https://domain/method/param
header:
{Authorization:'' }

Now, the response of the rest api is in json array as below:

[{"a": 1, "b": 2}]

My query:

  1. Is the above format of response is correct (which I doubt)?
  2. Because I am not able to get the data even though the API is returning with 200 status code. So, is it the issue with API or something to do in my code. I am just simply using request api of node js to consume this service.

In my view the response should be a single JSON object like:

{data :[{"a": 1, "b": 2}]}

So, for more understanding I am adding the screenshot of response in my code and through postman.

response when hitting through code

response when hitting through postman

Upvotes: 0

Views: 833

Answers (2)

Binit Ghetiya
Binit Ghetiya

Reputation: 1979

There is no fix response or error JSON objects, but as per your comfort you can create your own.

Here below i'm showing you my response and error objects that i use while developing rest APIs

Success Response object

 {
   "code" : 200, (any success code 201,301)
   "data" : object or array 
 }

Error Response object

 {
   "code" : 404, (any error code 400,422,500)
   "error" : "error message"
 }

Here i have took code, you can also you status which will be true or false.

Upvotes: 1

Novuw
Novuw

Reputation: 31

JSON only returns as JSON, so the array itself is going to be paired like you have in your second view. Is that what you were asking?

Upvotes: 0

Related Questions