Reputation: 1768
For example, when I make a http request below :
GET /myBox
And expecting a json format data response:
{
a: (expecting a number),
b: (expecting a string),
c: (expecting 'apple', 'orange', or 'banana')
}
Then let's suppose our server program would never made mistakes. Should we need to verify the response's content data in case some net mistake? And if we should , what's the most efficient way to verify every response data.
Upvotes: 0
Views: 725
Reputation: 99495
In general, I think you can reasonably expect the response to be correct. Most developers would. If you deal with multiple servers and multiple implementors of those servers implementing the same protocol, maybe you can't trust it.
One way to validate the response in that case would be to use something like json-schema
.
However, if you don't deal with many servers, maybe you can ask yourself under which conditions the response might be wrong. Do you have a reason not to trust it?
Upvotes: 1
Reputation: 562
It would be best to create a predictable JSON payload that comes from your server in both success and error cases. For example if we run into an error perhaps a payload like so:
{ error: true, errorMessage: 'Some error message', payload: null }
For network related errors you need to handle the appropriate HTTP status codes in your client application. More on that here: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
If you want to develop a ridged schema for data passed from server to client you might want to look at a format that is more suited for that like Protobuf.
See:
Upvotes: 3
Reputation: 103
You kind of verify the response data when you set the data type of the fields that you expect in the response object.
for example:-
suppose this is your class that you are expecting as a response object:-
public class example {
private String field1;
private int field2;
private bloolean field3;
}
If in the above fields you get field1 as a long instead of String then you will get an error which kind of checks whether the server is giving your expected response or not.
Upvotes: 0