how to use pre request script properly #992
-
hello guys I wrote a pre request script:
I use environment variable but i get an error: What does this error mean? And have anybody an idea to solve i? thanks |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 14 replies
-
What are you trying to do with the script? If you want to set the url, method, body etc you need to use a setter for each of these like req.setUrl, req.setHeader, etc. |
Beta Was this translation helpful? Give feedback.
-
I write to this format:
but i get : Error invoking remote method 'send-http-request': AxiosError: Request failed with status code 400 i would like to login to azure ad webapp with bearer token It is working in postman, but i can not make it to the correct form to bruno |
Beta Was this translation helpful? Give feedback.
-
I finally got this working. A few things that tripped me up that might help others:
const axios = require('axios');
const tokenUrl = bru.getVar('TOKEN_URL');
const clientId = bru.getVar('CLIENT_ID');
const clientSecret = bru.getVar('CLIENT_SECRET');
const scope = bru.getVar('CLIENT_SCOPE');
const axiosInstance = axios.create({
baseURL: tokenUrl,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
});
const data = {
grant_type: 'client_credentials',
client_id: clientId,
client_secret: clientSecret,
scope: scope,
};
axiosInstance.post('', data).then(response => {
const accessToken = response.data.access_token;
console.log('Authentication successful!');
console.log('Access token:', accessToken);
// Use the access token for subsequent API requests
bru.setVar('ACCESS_TOKEN', accessToken);
})
.catch(error => {
console.error('Authentication failed:', error);
}); Hope this helps someone! |
Beta Was this translation helpful? Give feedback.
-
Hi guys, I had similar problems:
Looks like a bug, right? Thanks, |
Beta Was this translation helpful? Give feedback.
i am not sure that u could help me more
my response: 401
response bodi: empty
auth: bearer: {{ACCESS_TOKEN}}
my code: