Value of an environment variable is reset to null in Postman - postman

I have a login request that returns a token that gets saved to environment variables via the following script added in Postman - Tests tab:
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("token_abc", jsonData.access_token);
Another request uses that token in the Authorization header as Bearer {{token_abc}}.
But, it seems the token is valid only for one-time use: after request #2 is sent, the value of token_abc is reset to null in Environment variables - Current value.
What could be causing this? Because this is not how the application usually behaves - the token should be valid for 24 hours. Is there a Postman setting I'm not seeing somewhere? I'm using Postman 8.6.1.

For some reason unknown to me there are 2 "postman" object in the postman sandbox that you can use to set environment variables.
postman
pm
Both can call a function for setting variables on the variable scopes in postman. From what I gathered on forums discussing various postman problems, the usage of the postman object gets discouraged in favor of pm.
See if this helps:
//check if you get the value you expected first
console.log(jsonData.access_token)
pm.environment.set('token_abc', jsonData.access_token)
Since your variable gets reset somewhere, there must be code in your script responsible for that. You mention the variable gets reset after request #2, so the first place to look is in the "Tests" tab of your second request. You can use console.log(value) to output the value of that variable to console on various places in that script in order to further pinpoint the exact location in code where it gets reset.

Related

setNextRequest() in Postman

I am trying to set a request after a certain request in postman, but It is not working as I want it to be. I have read through Postman documentation but got no luck. Plus, how do I get postman's request ID?
I am using the given JavaScript in the Test tab, and in postman documentation it says it should work. But no luck.
postman.setNextRequest('Login');
postman.setNextRequest('Login') will work only inside the Runner. Also, the requests need to be in the same Collection.
Even if the request is in another folder (while still in the same collection), you can reference it in setNextRequest(), without having to specify any folder.
And to answer your second question, "how to get the Postman Request Id?"
Use this pm.info.requestId which will return string value, you may set that in environment variable as well, like pm.environment.set("rID", pm.info.requestId)

Is there any way to access response / request details for the last executed Postman call

Is there any way to access the response and request properties of the last run postman call using pm api's. In other words, does postman keep any historical log which is accessible till at least the last request.
Something like:
pm.lastResponse.body or pm.lastRequest.Url
You can save the response from the last call to an environment variable and get it on the next one
http://blog.getpostman.com/2014/01/27/extracting-data-from-responses-and-chaining-requests/

Postman inconsistent behaviour with cURL request

I have the following API downloaded in JSON from Swagger Editor:
PUT http://10.37.64.243/m2m/fim/items/fim:device:manager/operations/getAllDeviceTypes?exclude={{exclude}}
with exclude being an environment variable set as : href,metadata,name,arguments
Nominal use requires basic authentication, in this case it works and I get a JSON body with expected result. It properly works in Postman (so I think my import is correct)
If no authentication is provided:
in Swagger Editor : nominal behaviour, request is rejected with error code 401
in Postman : UNEXPECTED behaviour, I end up with status code 200 and it returns a response body identical to the one that I get when authenticated
if I generate the cURL code snippet from Postman and launch it out of Postman: nominal behaviour, I get the same error as the one I get in Swagger Editor (the one expected)
Why do Postman behave differently from the cURL request ??
I probably do something wrong, but I can't figure out what
Thanks for any help
Alexandre
I finally found out that the server returns a cookie that holds authentication validation. So after a valid authentication, whatever the request (with or without authentication) it will be considered as authentified.
Unfortunately, the only way to overcome that problem is to remove the cookie by hand through the "Manage cookies" window. Postman does not implement a function that erase it (even through the pm.cookies and pm.cookies.clear() function).
Postman developpers are aware of that, but there's no scheduling for this feature ...
EDIT: the feature is followed here https://github.com/postmanlabs/postman-app-support/issues/3312

Postman - Setting a cookie from a global variable with "Tests" code

What i want to do is to take a cookie value I've received after a "GET" message , set it's value as a global variable, then set it as a session cookie for a specific host, and later on use it in other requests.
Currently i'm doing this manually via this menu:
Is that possible?
What you need to do is in the Test on response store the value you want to use in a global variable postman.setGlobalVariable('key', value) then in the request you want to use it as a cookie, pass along the value in the header as a cookie where:
Cookie | cookieName={{key}}
Check out the MZN docs on Cookie to see how to send multiple cookies in one header.
Doing this is well documented on the POSTMAN site. You require the interceptor plugin and the instructions to use it are here:
https://www.getpostman.com/docs/interceptor_cookies

JMeter - Changing a Cookie's Value during a Test?

I have created a test plan in JMeter *(Version 2.13). I was able to add a Cookie Manager to the plan and have my cookies preserved between HTTP Requests, which works just fine.
What I'm trying to do now is, if possible, preserve one Cookie from the start (*Called "JSESSIONID") and then have another Cookie (*let's call it "MYID") which can be modified during the test.
My 1st try at this was like the following:
+Thread Group
-HTTP Request Defaults
-HTTP Cookie Manager
+HTTP Request - Goto HomePage
-HTTP Cookie Manager - Set MYID="server_1"
+HTTP Request - Load Login Page
-HTTP Cookie Manager - Set MYID="server_2"
....and so on for about 3 more HTTP Requests....
But, doing it this way only kept the original Cookies which were captured by the initial Cookie Manager which was set right under the Thread Group element.
Apache's site warns that:
"If there is more than one Cookie Manager in the scope of a Sampler, there is currently no way to specify which one is to be used. Also, a cookie stored in one cookie manager is not available to any other manager, so use multiple Cookie Managers with care."
Another test I did, that was similar to the first except I added a User Defined Variables element to the start of the test which had a variable for the MYID Cookie set to one server, and then about 2 or 3 HTTP Requests down I had a second User Defined Variables element, this time setting the variable to another server. But, after reading a bit more on the User Variables, JMeter only reads User Defined Variables ONCE, at the beginning of the test. So the variable I set last would remain as whatever I set it to in the 2nd User Defined Variables section. So that way won't work to change the Cookie value during the test...
I also, read this from ApacheJMeter's site, here Using Variables to Parametrize Tests, which seems closest to what I am trying to achieve. But, from their example it seems as though it's for modifying variables before the test starts, like for example setting a variable from the command line...
Summary:
To sum things up, what I am trying to achieve is to send my HTTP Requests with 2 Cookies:
1. JSESSIONID
2. MYID
Where JSESSIONID will remain the same from the very start, and MYID which I want to change it's value during test *(multiple times if posible).
Let me know if that does not make sense and I will try and explain further.
Any thoughts or suggestions would be greatly appreciated!
Thanks in Advance,
Matt
You can use Beanshell scripting for cookies manipulation as:
Add a Beanshell PreProcessor to your HTTP Request
Put the following code into the PreProcessor's "Script" area:
import org.apache.jmeter.protocol.http.control.Cookie;
import org.apache.jmeter.protocol.http.control.CookieManager;
CookieManager manager = sampler.getCookieManager();
for (int i = 0; i < manager.getCookieCount(); i++) {
Cookie cookie = manager.get(i);
if (cookie.getName().equals("MYID")) {
cookie.setValue(vars.get("foo"));
manager.remove(i);
manager.add(cookie);
break;
}
}
Above code will substitute "MYID" cookie value with "foo" JMeter Variable value via direct calls to CookieManager class (see API documentation for all possible methods and fields)
Check out How to use BeanShell: JMeter's favorite built-in component guide for advanced information on Beanshell scripting in Apache JMeter.