forgive my lack of tests and error logic. I know my pre-request script is working but for the life of me I can't figure out why the subsequent POST request fails with "Invalid token". I'm using Postman to execute a pre-request script for auth and it's returning 200 and is giving the { id } from the res. BUT!! when I set the env variable of { token } and then call on { token } from the header of the actual POST request, I'm getting an "Invalid token" response, 401.
I checked the logs and the POST request header is matching the { token } that was returned in my pre-request script. Why would the server reject it?
const auth = pm.environment.get('auth')
const user = btoa(`${pm.environment.get('username')}:${pm.environment.get('password')}`)
if (!auth) console.log('Missing authorization endpoint')
if (!user) console.log('Missing credentials')
if (pm.environment.get('token')) pm.environment.set('token', '')
const echoPostRequest = {
url: auth,
method: 'POST',
header: `Authorization: Basic ${user}`
};
pm.sendRequest(echoPostRequest, function (err, res) {
const { id } = res
pm.environment.set("token", id)
});
You would need to reference the access_token
key to set the value in the environment variable. It looks like you're setting the whole response body as your token value.
pm.sendRequest(echoPostRequest, function (err, res) {
const { id } = res.json().access_token
pm.environment.set("token", id)
});