rest test api code example
Example 1: dummy api json
var xhr = new XMLHttpRequest();
xhr.open("GET", "https://reqres.in/api/products/3", true);
xhr.onload = function(){
console.log(xhr.responseText);
};
xhr.send();
Example 2: how to test rest api
First, I verify if each REST API end-point is working as
expected.
- I use POSTMAN for manual API testing and use
RESTASSURED library in Java for automation.
- I send GET, POST, PUT, DELETE type of requests and
verify response status code and response body.
- I also do positive/negative testing of API.
When I do positive testing,
I send
valid request parameters,
valid headers,
valid request json body to valid url and verify that
response status code is 200 and
Json response body data is as expected.
When I do negative testing,
I send
invalid request parameters,
or invalid headers,
or invalid request json body and verify
response status code is not 200.
and Json response body contains error message. (for example, there are 100
employees numbered from 1 to 100, I send 101 as an end-point and I should be
able see 404 Not Found. The requested resource was not found.
Example 3: what to test in api
Request Payload has
ENDPOINT (Specific URL where we send our requests to a certain web service. )
METHOD (Get•Post•Put•Patch•Delete)
HEADERS(Represents the metadata of request
or response. Like basically we can verify
the object type we are sending etc )
PARAMETERS(Parameters are to specifiying
even more about the request we are sending
like what specific resources needs to be returned etc.)
BODY(Body is where we sent or received the request we made)
For the repsonse
Basically we are checking response body
to verify if request matches with response.
In the response we are verifying
(body, status code, header, response time,
test structure of json against the given jsonSchema)
Example 4: dummy api
React Fakers is a collection of dummy data from the most popular dummy data
providers such as Json Place Holder, Faker, Pokemon, etc,
for application development testing.
npm i react-fakers | yarn add react-fakers
Example 5: what to test in api
Basicall we are checking response body
to verify if request matches with response.
After that we are writing SQL query
to double check if database matches.
We can say happy path if all pass