how to set token automatically in postman - api

Want to set token automatically in postman. On a login Api call set token in postman automatically.
want to clear then postman variable and set new token given by as Api response

Set the token variable in collection ver in postman.
write the given code in api test part
pm.collectionVariables.unset("<token _name>");
pm.collectionVariables.set("<token_name>", pm.response.text());
run api

Related

Authorization with cookie - ROBLOX API

So Im trying to get a list of transactions from a group, but not sure how to implement the .ROBLOSECURITY cookie value into my header.
So the .ROBLOSECURITY is a cookie therefore it is assigned in the headers.
The way this should be used is:
Header = Cookie
Value = .ROBLOSECURITY=your cookie
Here is an example built in PostMan, not sure what your using:

How to automatically send JWT token collected in previous request in Postman

I'm creating REST API (Symfony 4, FOS Rest bundle) and for testing I'm using Postman app. Problem is that at login request I get JWT token and later, in every other request I have to pass it back as part of Authorization header, as Bearer token. And since this token changes with every login I have to manually copy/paste token value after every login (when token expires).
Can that be avoided somehow and done automatically?
First, after successful authorization - login call returned the JWT token it has to be stored into some variable. When editing login request, there is a "Tests" tab.
Here we can put JavaScript code that will be executed after request is executed, so we will enter the code like this there:
var jsonData = JSON.parse(responseBody);
if(jsonData.token) {
pm.globals.set("jwt-token", jsonData.token);
}
Or, shorter version, as #Danny Dainton suggested:
pm.globals.set("jwt-token", pm.response.json().token)
We are collecting response and storing "token" value to global variable called "jwt-token".
If you use older version of Postman this code should look a bit different - storing variable should look like:
postman.setEnvironmentVariable("jwt-token", jsonData.token);
(Here storing as environmental variable vs. global variable in example above - both types should work. Use what you need).
So now, token value will be stored. Then we have to use it with other requests.
Edit all other request that must pass JWT token. Go to "Authorization" tab, select "Bearer Token" authorization type and for value just enter {{jwt-token}} .
Again if you are using older version of Postman and don't have that "Bearer Token" type go to "Headers" tab instead, add new header with key "Authorization" and for it's value set Bearer {{jwt-token}}
That's it. Now you have to execute login request only once and JWT token will automatically be used in all other request.
And if you face some issue, you can use console to print debug info. Add in you code i.e.:
console.log(jsonData.token);
And from main menu go to View -> Show Postman Console to open console window where will you get console.log output.

How to call API with AntiForgeryToken using Postman in IdentityServer ASP.NET Core

I'm trying to test my API with Identity Server Asp.net Core using Postman.
This is the way that I'm trying to do:
First request HttpGet to https://localhost:5000/Account/Login and in response body I received: <input name="__RequestVerificationToken" type="hidden" value="CfDJ8MoS9upoM4dNp8Kx-AdvA-uYr13_PAkuMZpzYMV8UmxZq5GdLTvN-Ht5NpTLmPtlhL5d5z2Hu2vUJoJGhk1AMlARDcOwqgq7Cef1dfQL_vl4tIFM4kx9RZPz8DHU26-U9qLnKAIstZgR42-1FuGNh24" />
And in Cookie (not sure for what it is though):
Then HttpPost to https://localhost:5000/Account/Login with RequestVerificationToken with token received from body HttpGet request.
And always error 400 as you can see at screen shot above.
In Visual studio I can see that some request was catched but clearly was incorrect.
If I'll remove attribute [ValidateAntiForgeryToken] then of course everything works fine but obviously because that validation is disabled.
You'd need to do followings to send such a request:
1.) Enter __RequestVerificationToken key value (don't forget double underscores) into x-www-form-urlencoded
2.) You need to add .AspNetCore.Antiforgery cookie to the Cookies section in Postman.
For example like this
.AspNetCore.Antiforgery.1XHiLFgQI2w=your cookie value; Path=/; Domain=localhost;Expires=Session;
You can find .AspNetCore.Antiforgery cookie in Application section in Google Developer Tools
.AspNetCore.Antiforgery cookie in Google Developer Tools picture
Add cookie in Postman picture
Just spent a lot of time on this.
I did several things:
Setup an Environment and added a variable.
Added a pre-request script that...
Uses pm.SendRequest to Get the page
uses cheerio to find the first input field named __RequestVerificationToken and get its value
set the environmental variable to the value retrieved from the field
send the form data (since I'm using asp.net core, the values for the model), as x-www-form-urlencoded
and last, but not least, I added __RequestVerificationToken as one of the key value pairs in the form data and set it to the use the variable already setup
The main reason I am posting this answer is the last, I saw a lot of things on the web that indicated that name was supposed to be RequestVerificationToken, and that doesn't work, just leads to a 400 response (bad request).
In postman, you’d need to set the content type to form url encoded.
And send the request Verification token in the header as "RequestVerificationToken"
However, if you just need a Bearer token then you need to call
POST https://<your identity server>/connect/token with the

Get information about the user google plus api by token

I use the application android and get a token when making a request:
oauth2:server:client_id:xxxxxx.apps.googleusercontent.com:api_scope:profile email openid
After that I want to get information on this token on the web server.
I'm trying to use:
https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token=xxx
But I do not get to get the data, how can I get it?
To get a real token, you need to correct the request for url:
https://www.googleapis.com/oauth2/v4/token
and pass parameters by the POST method:
grant_type=authorization_code&client_id=xxxx.apps.googleusercontent.com&client_secret=xxxx&code={code}
code - a token of the type previously obtained 4/xxxxx

Use postman to get a token from a request and send it to another

I am using postman to do some testing on a REST API.
To login I use a post request who respond with a token I need to keep to use it with another request.
I did it that way :
and then I want to use it in another request who need that token in the header :
I seems that it's not sending the token. What am I doing wrong ?
Let's say the login response is:
{
"message": {
"token":"Some token value here"
}
}
There is a slight change in the latest postman and here is the syntax to set variable:
var data = pm.response.json();
pm.environment.set("token", data.message.token);
Read here more information:
https://learning.getpostman.com/docs/postman/environments_and_globals/variables/
The problem was that I was using postman.setEnvironmentVariable() instead of postman.setGlobalVariable().
I found the answer here
I have used the below script in the Tests tab of the postman and it worked for me.
pm.environment.set("access_token", JSON.parse(responseBody).access_token);
After setting the access token in my first API, I am passing that access token in my second API.
You can save token to an environment variable and access in any requests under that collection. May be this link will be helpful:
Extracting data from responses and chaining requests
Go to Login API
Go to "Tests" tab.
Add folowing script. This script will set variable "AuthToken" with response token.
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("AuthToken", jsonData.data.accessToken);