Skip to main content

HTTP Requests with axios

You can use axios and other third-party libraries from your monitoring scripts.

You can invoke HTTP requests and also perform assertions using axios for testing your APIs.

Following is an example script for invoking http requests using axios and asserting the responses.

const axios = require('axios').default;
const { expect } = require('chai');

//get example
await axios.get('https://your-server-url.example.com')
.then(function (response) {
//console.log(response);
expect(response.status).to.equal(200);
expect(response.data.message).to.equal("hello from backend"); //asserts json response property value
expect(response.headers['x-environment']).to.equal('prod');
})
.catch(function (error) {
console.log(error);
expect.fail('HTTP get operation failed')
})
.finally(function () {
//finally block
});

//post example
const payload = {
environment:'dev'
}
await axios.post('https:/your-server-url.example.com', payload)
.then(function (response) {
expect(response.status).to.equal(200);
expect(response.data.message).to.equal("Your server response");
})
.catch(function (error) {
console.log(error);
expect.fail('HTTP post request failed')
})
.finally(function () {
//finally block
});