How do i get the csrf token from scenario 1 response using Karate framework? - karate

I have application api url which has passed as Background with Basicauth. Using that i need to get an csrf token. I tried with responsecookies. Nothing is coming up.
Can you please let me know how to get the csrf token from that specific api. I am getting actual application response but not the cookies.

Please read the documentation and go through the demo examples. It depends on the response. There are 2 possibilities, cookie (or header) OR the response body itself (plain text or JSON / XML).
So it is up to you to extract it and then store it as a variable, and use it to build headers for all future responses.
Please refer to the demo example:
Given path 'signin', 'token'
When method get
Then status 200
And header X-CSRF-TOKEN = response
In this case, the token happened to be the entire response string.

Related

How to pull the token from Get request's response and input the same to Post request in JMeter for Rest API?

I am a beginner in JMeter, trying to perform load test to my Rest API. First, the Get request gets the access_token as below:
Then the following Post request runs with that authorization token to produce the Json, but I am missing something so the Post Request is failing as Authentication denied as below,
Added the Regular expression extractor to extract the token from the Get request's response as below:
Then storing it in the HTTP Header Manager in a variable as below
I am missing to link the value to the Post request, I dont know how to do it, please help.
In header manager, instead of access_token, the header name should be Authorization. Please reconfirm this with developer team / retry this same request in postman
The regex expression used should look something like this
"access_token":"(.*?)"
Also, another pro tip: because the response to getToken api call is JSON, you can use JSON Post Processor to extract the access_token by saying something like $.access_token. It is much clean way to process JSON object.

API, passing bearer token to GET HTTP URL

I have a OAuth2 (bearer token), but how do I pass it to the endpoint? More precisely, how do I include the bearer token in the URL.
Example URL:
https://api.twitter.com/1.1/search/tweets.json?q=%23superbowl&result_type=recent
Example Bearer Token:
AAAAA5n0w5n0w5n0wMyL0ngBe4r4rT0k4n
How do I use my bearer token to authorize my requests?
Example Incorrect Usage:
https://api.twitter.com/1.1/search/tweets.json?q=%23superbowl&result_type=recent&bearer=AAAAA5n0w5n0w5n0wMyL0ngBe4r4rT0k4n
You can do it in two equivalent ways:
by using the URL access_token parameter:
https://base.url?access_token=f4f4994a875f461ca4d7708b9e027df4
or by adding the Authorization header:
Authorization: Bearer f4f4994a875f461ca4d7708b9e027df4
Note: Whether two or only one of these methods works sometimes depends on the specific implementation of the web application (including the services and libraries used to create it).
https://YourApiUrl?access_token=0db69822-0d02-4c17-8c39-d3b818bee184
Append the valid bearer token as value to the key "access_token". This worked for me.
On Postman go to:
Authentication tab
Select type: Bearer Token
Paste in your Token
If you want it in the URL too like you mentioned, just pass it in as parameter in the GET request.
Follow these steps:
https://base.url and set the request type.
Now under the url there is an Authorization tab --> click it.
When you in the Authorization tab select the Authorization type and fill the required token fields.
You are using the OAuth 2.0 but there several more API Key / Bearer Token / ..
Before sending the request Make sure to send a body if the request is waiting for a parameters.
Body tab under the url field --> click it.
Selected the required datatype none / form-data / raw / ..
If the raw datatype was selected make sure to select the schema-type
Text / javascript / JSON / HTML / XML
Here is a quick explaination video:
https://www.screencast.com/t/E3igDLQE
FYI,
https://Endpointurl?Authorization=Bearer 00D2w000000kc19!AQ0AQAXCu2pbsBn4YvKuvUbNieCGmG2CVL4lqVaJruAXKZYWuAQiuxtuMiOgvGw2KQ.E4CJtkOdUw0_Q7yUzQh_3XAgwWQfd
Hope you understood how to pass the token

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

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);

CORS authorization on google sheets API requests

I'm trying to read data from a google spreadsheet by retrieving a cell based feed via the sheets API.
The spreadsheet is private, so I'm using oauth 2.0 to authorize my requests.
Retrieving basic infos about my drive account via https://spreadsheets.google.com/feeds/spreadsheets works fine, but when I try to access the data from on of my spreadsheets directly via
XMLHttp GET Request to https://spreadsheets.google.com/feeds/cells
I'm getting an "No 'Access-Control-Allow-Origin' header is present on the requested resource" error.
I've set the correct Authorization Token via
xhr.setRequestHeader('Authorization', 'Bearer ' + token);
and tried to "activate" CORS via
xhr.setRequestHeader('Access-Control-Allow-Credentials', 'true');
To no avail.
Any tipps on how to access this resource ?
Thanks !
Fixed this very same issue just today. And I did not need to enable/activate CORS as I've read that some firewalls will strip out the headers for security. http://promincproductions.com/blog/server-proxy-for-cross-site-scripting-cors/
In a global part of your js code, add in a function ...
window.googleDocCallback = function () { return true; };
Then, to the URL in your AJAX (GET assumed?) request, if you have no URI params, append ?callback=googleDocCallback
and if you do have other params,
append &callback=googleDocCallback
For more info, please see: https://jvaneyck.wordpress.com/2014/01/07/cross-domain-requests-in-javascript/
Google Sheets API has a known bug that doesn't support CORS for POST requests. Can you confirm that you are in fact making a GET request and not a POST/PUT/DELETE?
You need to send a GET request. Make sure that there are no headers sent (not even content-type headers).