How to get auto-generated headers with pm.request.headers in Postman? - postman

I have a Postman pre-request script which uses pm.sendRequest to call an endpoint that uses the same authorisation as the current request. I tried to use pm.request.headers in the script to get the Authorization header so I could add the same header in the pm.sendRequest call, but it doesn’t return the auto-generated headers, only the headers I’ve set manually. Is there any way to access the auto-generated headers in a pre-request script?
I tried
header: {
"Authorization": pm.request.headers.get("Authorization"),
"Content-Type": "application/json"
},

I do not know how to access the auto-generated headers, but you can actually access the data from the Authorization tab of Postman and thus also the access token:
pm.request.auth.parameters().get("accessToken")
I use this for setting the token parameter to a Postman variable named token_param when hitting the OIDC token introspection endpoint after having authenticated via OAuth 2.0 Authorization Code Flow in a browser tab from Postman:
pm.variables.set('token_param', pm.request.auth.parameters().get("accessToken"))
You can also check what's inside the auth object by calling the toJSON method:
console.info(pm.request.auth.toJSON())
That way I found the accessToken:

Related

HTTP 401 Unauthorized when calling web method using a personal token as authorization

I am trying to call this web api using postman. In order to use it you must apply for a personal token via email before (see here at the beginning of the page indicated as "Personal token request", you must send an email requesting a personal token).
So I have requested a personal token and once I have it I am trying to call above indicated method, this one.
So I configure postman as below screenshots show. I only fill in the Authorization (with my token) and the headers tabs. I indicate GET as verb and as url: https://api.esios.ree.es/indicators
Authorization:
Headers:
When I send the request I get error:
HTTP Token: Access denied.
UPDATED:
It doesn't work...
UPDATED - ATTEMPT #2
Finally I have solved it by setting authorization "No Auth" in Authorization tab and instead set Authorization token in the header tab.

AWS Serverless API Request

Whenever I begin to use the AWS_IAM authorizer on my function, my API GET request - which is being made from a web client - receives a No 'Access-Control-Allow-Origin' header is present on the requested resource. and I'm getting a 403 status code. When I remove the aws_iam authorizer, the API request - made via the same web client - succeeds. So I'm pretty sure it's not actually a CORS error since I've double checked that my API has cors enabled. I'm not sure if I'm using the AWS Javascript SDK wrong or if I'm not generating proper roles for AWS_IAM in my serverless.yml config.
When I run the request from postman with my personal access keys from AWS, the request succeeds. When I console.log the keys from the AWS.config.credentials and use those in postman, the request receives an invalid token error message. Maybe I haven't generated the proper role for my federated identity pool?
I'm using aws4 on the front-end to sign the request, and I use axios to make the request.
Any ideas?
Axios will make a pre-flight request. It's an OPTIONS type and expects a 200 response before it will make the GET request. It also needs to return the CORS header:
Access-Control-Allow-Origin: *
Postman won't make the same call automatically but you can use it to test that request. Ensure that your API is accepting OPTIONS request methods for that route and that it returns an empty 200 response with the CORS header and I think you'll be good to go.
AFAIK, the OPTIONS request needn't be authenticated. Just give a green light. It may be the case that your client is making calls to this endpoint as OPTIONS requests that are being authenticated, adding a second layer of confusion, or you may need to add an Access-Control-Allow-Headers to allow an Authorisation type header if AWS is using one.
Hard to give more information without debugging but I would start with an OPTIONS request.

How do i set up a bearer token in postman from an environment variable?

I have set up a collection in PostMan and am able to save my bearer token value to an environment variable successfully using the following test
var jsonData = JSON.parse(responseBody);
pm.environment.set("mytoken", jsonData.token);
but how do I set up a new call to use it?
I have tried adding a Header with
Authorization Bearer <mytoken>
but when I Post the Status is 401 Unauthorized
You can use Tests tab to write your code which updates the Environment variable, as explained in this link. Read more about Test scripts here.
Assuming the response of the auth call is:
{
"token": "woaejrlajfaoidhfalskdjfalsdijfasd"
}
Then, in Tests tab, you can write like:
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("token", jsonData.token);
This will update the variable token whenever you trigger the auth call. This token variable should be used in headers of all the API calls, to update automatically.
Do also check inheriting the auth.
In the headers I needed to use
for the key
Authorization
for the value
Bearer {{mytoken}}
Summary:
Create a variable to store Auth Token value in single place to use throughout your collection.
Set default method for Authorization for your entire collection.
Instead of setting the Authorization header for each request set the Authorization on each request to use "Inherit auth from parent" to automatically populate the request with the proper auth headers.
You can define variables in Postman environments and collections in order to simplify your requests by setting a value in one place and reference it in as many places as necessary. So you can create a variable for your Bearer Token value. Do this by editing your collection and going to the Variables tab to add a new variable.
Also while editing your collection go the Authorization tab to set a default authorization for all requests within your collection. You can set the Authorization Type for your collection to Bearer and set the Token value to be your defined variable. This will allow you to use the same authorization token for all of your requests within your collection:
Then in order to use the collection's default method of authorization, you will need to set the requests within that collection to set the Authorization Type to "Inherit auth from parent". Doing this will allow you to not have to deal with adding the Authorization header manually on to each request. Each request within the collection with the "Inherit auth from parent" authorization type selected will automatically populate the request with the proper headers for authorization if you have defined a default option for the collection like in the previous image.
Cheers!
I use a script after login post into tests tab like below;
let jsonData = JSON.parse(responseBody);
pm.collectionVariables.set("jwt_token", jsonData.data.token);
and create a collection variable like following;
Like the way Kristen, said. Or else download latest postman desktop application, in that in authorization they have an option to add bearer token in the header
pm.environment.set("JWT",pm.response.json().token)
Note : JWT is the environment variable you set in your environment

token not being sent in pre-flight request Ionic2

I am working on an app in Ionic 2 using DRF as API service. For authentication purpose I am using JWT. I am sending auth token with each request as Authorization: jwt [token]. In Postman, API is working fine.
Now when I am testing it in browser it is not working and I figured out that it is probably not working because the JWT auth token is not being sent in the OPTIONS request as a pre-flight. So how do I tackle this problem.
In the Ionic latest versions if you are using ionic serve commnad then you must have to use Proxies to prevent Preflight and CORS Issues,
First add API path and URL in ionic.config.json file like
{
"name": "APP-NAME",
"app_id": "",
"proxies": [
{
"path": "/api",
"proxyUrl": "http://example.com/api"
}
]
}
Now, while calling your API from http use the /api URL instead of the http://example.com/api like,
....
this.http.post('/api', data, {headers:headers}).map(res=>res.json()).subscribe(data=>{
console.log(data)
}, err=>{
console.log("Error!:", err.json());
});
....
After making the above changes you must rerun command ionic serve.
Still, if you are getting issues then refer Handling CORS Issues In Ionic and https://ionicframework.com/docs/cli/configuring.html
In CORS preflight OPTIONS response Cross-Origin-Allow-Headers should match that of the request.
Cross-Origin-Allow-Headers: Authorization
Actually the problem was that the OPTIONS api was not readable without the Authorization token so we added the readonly Auth level for the OPTIONS and GET api.

AWS Custom Authorizer - Get token from cookie

I'm currently building a web application whose backend is purely build in API Gateway/Lambda. I build a custom JSON Web Token (JWT) authorizer to authorize the users. At the moment I'm passing token in header field.
Unfortunately, I'm only able to define a header field in which the token is send to API Gateway.My applications stores the token in a cookie.
Is there any option to access the cookie directly so that it can authenticate using lambda.
For example:
Now I'm passing:-
method.request.header.Authorizer
But I need somehting like this :-
methods.request.header.Cookie
Any workaround ? Thanks!
Now you should be able to access all the headers including Cookie header, using Custom Authorizers of the REQUEST type. Recently AWS introduced this feature to allow access to more than Token Header.