Use case: Before making an API call, get the latest OAuth token and update it in the environment variable
(async () => {
try {
//Set environment variables and encode credentials// Send POST request console.log("Before sending request..."); const response = await pm.sendRequest({ url: tokenEndpoint, method: "POST", header: { "Content-Type": "application/x-www-form-urlencoded", "Authorization": `Basic ${encodedCredentials}` }, body: { mode: "urlencoded", urlencoded: [ { key: "grant_type", value: "client_credentials" } ] } });
console.log(“Response received:”, response); // Log the entire response object
// Check if response exists (optional) if (!response) { console.error("Error: No response received from server."); return; // Exit the function if no response }
// Code to read response and update the “access_token” environment variable
} catch (err) {
console.error(“Error fetching new token:”, err);
}
})();
The log statement in bold prints that ‘response’ is a function. Here is the exact message :
" Response received: {test: “[Function]”}"*
Because of this, I am not able to read any propertie from the response object.
Any idea what I might be doing wrong?