TESTEROPS

A pragmatic approach to QA and OPS

GET and POST Requests

In our previous post we saw how we can install Chakram API Testing library. In this section, we’ll see how we can make a simple GET request in Chakram.

 

Internally, Chakram utilizes the requests library of node.js. As such, the methods of the original library are implemented in almost the same fashion. A simpleGET request using Chakram can be done with the help of the chakram.get() method, which accepts three parameters :

  • The request URL – this is the specific JSON REST endpoint to which you want to send a GET request.
  • The request Body – if you want send some specific or custom parameters or headers along with the request.
  • The request options – Any request options as an optional third parameter.

The first parameter, ie. the request URL is mandatory, while the rest two are optional.

 

Examples

There is an excellent REST API endpoint testing example mentioned on this website . We’ll be using the JSON REST endpoints provided here to give an example.

 

To make a simple GET request in Chakramyou can do

 


const chakram = require('chakram');

describe('Make a GET request',()=>{

it('makes the first GET request',()=>{

return chakram.get('https://reqres.in/api/users?page=2');

});

});

 

The describe and it syntax in the tests are borrowed from mocha which is the test runner for this framework.
Similarly a POST request requires similar kind of code. The POST method requires following three parameters

  • The request URL – this is the specific JSON REST endpoint to which you want to send a POST request.
  • The request Body – if you want send some specific or custom parameters or headers along with the request. This is a JSON serialisable object, unless it has been specifically turned off, in which case the body will be sent as a buffer or String.
  • The request options – Any request options as an optional third parameter.

In our next section, we’ll see how we can set up a small project and use expect to assert to response that we get from hitting the REST endpoints with either GET or POST methods.

%d bloggers like this: