I’m not very knowledgeable on how bearer tokens work, so please correct me if this is not a real issue with Postman.
At the moment, I can use the Interceptor to sync up cookies fine on one project that I’m working on (does not use bearer). I log in manually in Chrome and then any postman requests will work from there.
For a different project, it uses bearer tokens. I log in but it doesn’t automatically sync it and I don’t know why. I have to copy and paste the bearer token into request header in order for it to work. There is nothing unusual about the prefix for this bearer token, it just starts with ‘Bearer’ following by a long string. Should the Interceptor be able to handle this or do I have to do a pre-request script to set it?
If you do need to set a pre-request script, I did try to create one based on another forum post by changing it slightly. I needed to make it send a request with raw text body (login) then set a variable. However it didn’t quite work, I don’t remember why but when I will update this post when I can get the error message.
var tokenRequest = {
url: pm.environment.get("url") + '/token',
method: 'POST',
header: {
'Content-Type':'application/x-www-form-urlencoded',
},
body: {
mode: 'raw',
raw: 'grant_type=password&username=username&password=password'
}
};
// send bearer token request
pm.sendRequest(tokenRequest, function (err, response) {
if (err) {
throw err;
}
if (response.code !== 200) {
throw new Error('Could not log in.');
}
pm.environment.set("bearerToken", response.json().access_token);
});
Looking at it now, I think the issue may be the last bit where it says response.json
. It’s not json but plain text so what should it be instead?
Thanks for your help in advance.