API call with Axios

Axios is a promise-based HTTP client for the browser and node.js MomentumSuite Automate allows you to use the Axios library within your iOS, Android or Web project.

https://github.com/axios/axios

Example Code is here to use with MomentumSuite Automate Javascript language case. You can dynamically set your data in the same project and send your request to any API server with Momentum.

 

const axios = require('axios').default; axios.defaults.baseURL = 'https://api.myapp.com/v1'; axios.defaults.headers.post['Content-Type'] = 'application/json'; axios.defaults.timeout = 25000; //----------SET DATA---------- var loginUsername = "myjsuser@momentumsuite.com"; var loginPassword = "MyPass123*"; return new Promise((resolve, reject) => { var accessToken = 0; //----------SET REQUEST BODY---------- var requestBody = { 'username': loginUsername, 'password': loginPassword } driver.log("API Request Body: " + JSON.stringify(requestBody)); //----------SET REQUEST HEADERS---------- var config = { headers: { 'Content-Type': 'application/json' } } driver.log("API Request Headers: " + JSON.stringify(config)); //----------API CALL-1 - LOGIN ---------- return axios.post("/auth", requestBody, config) .then((response) => { driver.log("API SUCCESS response: " + Object.values(response.data).flat().join()); driver.log(response.data.yourResult.yourVariable); resolve(response); }) .catch((error) => { driver.log("API ERROR response: " + error); resolve(null); }); })