Itâs a pre-request script that requires Bearer Token authorization for the requests in it. I already have the token, I just need to pass it into a request like what I listed as an example.
I already know how to do a basic auth with similar syntax. My issue is around what the syntax for a bearer style authentication.
My mistake, I thought you were trying to get one with that request
It looks like you already added the word Bearer when setting the variable so you would just need to add a new Authorization header with the value in the example.
pm.sendRequest({
url: "URL of some type",
method: 'POST',
header: {
'Authorization': `${pm.environment.get('token')}`,
'Content-Type': 'application/json'
},
body: {
mode: 'raw',
raw: `Body Data of some type`
}
}, function (err, res) {
if (err) {
throw new Error(err);
} else {
console.log("auth was successful")
}
});
I would have thought that if you have the pre-request script thatâs getting the Bearer Token, wouldnât you just use that token value in an Authorization Header of your normal requests.
I donât really understand why youâre using this in another pre-request. I donât know your context and what you have in front of you so that only think that I can offer is a âguessâ.
Itâs due to some constraints that are being set from the BE due to code note present in the FE of the project. I believe once those constraints are removed, your solution should work with no issue
@Zachary: Great post!
It helped me to solve my problem.
However, when I first tried this I had an issue with the token.
Analysis of the ressonse headers revealed that the Bearer token was like this:
âBearer Bearer llkjh876976jjhgjhg874653hgIjâŚâ
The word âBearerâ was used twice, hence the authentication was KO.
So I deleted the âBearerâ part of the âvalue:â assignment