Http error code response

What is the best practice to return some error codes like 400, 412, 402 etc. ?
Can I return some kind of JSON if I'm creating restfull api ?
For example, if I send request POST .../cars/1 and I get 402 then should or could server return to me some kind of JSON if i need data? and I do not want to increase traffic on server too much.

tl;dr:
Should/could restfull api return data when server send error code for example 4XX [without 404]

Upvotes: 0

Views: 95

Answers (2)

Ankush Kocher
Ankush Kocher

Reputation: 1

you make your response object and send status in that for example. { "status":400, data:{ "error":"this is error response", "isSuccess":true } } and send response status in response

Upvotes: 0

Mardie
Mardie

Reputation: 1672

It should reply with that code STATUS so any client can understand it.

And then a good pratice is to check the Accept header sent by the client and reply a response encoded with any of the supported format. So for example the client has application/javascript It seems that it is expecting that format.

Upvotes: 1

Related Questions