[Help] Sending a request in Pre Request Script using axios #2268
-
Hi, I want to check by sending a request to my API if some work is done. Therefore I worked on the axios library to get this done. Unfortunately I am not able to simply send a request an evaluate the output. console.log("Axios-send-request: Pre Request - Start");
const axios = require("axios");
const urls = [
"https://httpbin.org/get",
"https://jsonplaceholder.org/users"
];
const url = urls[0];
console.log("url = " + url);
axios.get(url)
.then(response => {
console.warn("The response is:", response);
}).catch(error => {
console.error('The error is:', error);
});
console.log("Axios-send-request: Pre Request - End"); So I started with the above script stored as pre request script in a request. Here the issue is that neither the "The response is:" nor the 'The error is:' message appear on the console. Further more none of these messages do appear at all. Because I want to implement a comparison of the response the pre request script must wait until the response (or error) returns form the request. As I am new to JavaScript I even do not understand why none of the messages appear. :-( Kind Regards PS: I already read the following pages: |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi, After getting some help from my Colleges I found the following solution: console.log("Axios-send-request: Pre Request - Start");
const axios = require("axios");
// if you need to ignore invalid SSL certificate
const https = require('https');
const agent = new https.Agent({
rejectUnauthorized: false
});
const urls = {
httpbin: "https://httpbin.org/get",
jsonWork: "https://jsonplaceholder.org/users",
jsonError: "https://jsonplaceholder.org/users-xxx",
};
var headerObject = {};
// do not show the password unencrypted in the console
const username = bru.getEnvVar("AuthorizationBasicUsername");
const password = bru.getEnvVar("AuthorizationBasicPassword");
const encoded = Buffer.from(username + ':' + password).toString('base64');
// add new or overwrite existing header field Authorization
headerObject.Authorization = 'Basic ' + encoded;
console.warn("headerObject = ", headerObject);
const configs = {
empty: {},
ignoreSSLcert : {
headers: headerObject,
httpsAgent: agent
}
}
const url = urls.jsonError;
console.log("url = " + url);
const config = configs.ignoreSSLcert;
console.log("config = ", config);
async function sendRequest() {
await axios.get(url, config).then(response => {
console.warn('Success:', response.status, response.data);
}).catch(error => {
console.error('An error occured:', error);
});
}
await sendRequest();
console.log("Axios-send-request: Pre Request - End"); Perhaps this post help someone else. Kind Regards |
Beta Was this translation helpful? Give feedback.
Hi,
After getting some help from my Colleges I found the following solution: