setNextRequest() in Postman - 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)

Related

Sending a file to an API using Postman form-data

I'm attempting to send a file to an API I've been developing through Postman, but the API has repeatedly failed to receive it. I initially thought this was a middleware issue, and attempted to use different modules to fix it. However, I have since realized that this is actually an issue in Postman itself
When I send the request and then review the request body in the Postman console, I can see that it fails to send the file at all, listing it as undefined.
I have seen a few people having this same issue, but was not able to find a solution that worked for me. I have already tried unchecking the Content-Type header, as well as closing and reopening the tab and Postman.
EDIT: I did not receive an answer to this, but I was able to find a workaround - coding a super basic html form that made a post request to the relevant endpoint. This did work, so I'm going to say this is likely a bug or settings issue in Postman itself, rather than something to do with my server's code.
Pretty sure you were in this thread already, but just in case,
this answer, and this one might be what you need.

Access Cookies in Postman Native App Pre-Request Script

Apologies if been asked before. Tried a quick search and couldn't find.
Situation :
The api is using an authentication token as a cookie name "abc-auth" and this is returned when i hit a /login endpoint. It is returned as a set-cookie header in the response which postman the native app happily accepts and sets up as a domain cookie in the ui
I hoped to basically as a pre-request step hit the login endpoint if the cookie doesn't exist but not hit it if we're already authenticated. So we only login once for the 20 requests rather than 20 times
I had hoped to do this accessing the pm.cookies object which I believe is now fully baked in to the native apps ref -> https://www.getpostman.com/docs/v6/postman/scripts/postman_sandbox_api_reference
So was hoping to do something like this
console.log(pm.cookies.toObject())
if (pm.cookies.has("abc-auth")){
console.log("Found Cookie");
} else {
//send the request
}
Expected :
That it runs the first time logs in and then next time finds the cookie and continues
Actual :
It never finds the cookie. Printing out the cookie list finds an empty array. I am seemingly unable to check cookies from the script.
Anyone know what I'm doing wrong?
A lot of the docs refers to interceptor but as the chrome app is being retired and native app was meant to assume that functionality I would really like the answer to be contained within the native app
Thanks!
Would something like this work for you to do the check:
if (_.keys(pm.cookies.toObject())[0] === "abc-auth"){
console.log("Found Cookie")
} else {
//Do something
}
It's using the Postman cookies function but also the Lodash keys function (which is comes with the native app) It's basically assuming that the first key is the one you want - That's probably not right as it might have several keys.

How to access the true RAW request body from post man on a pre-request script?

I'm trying to authenticate myself against an API. This API uses the raw body from the request to create the hash that it will use to authenticate against my token.
For testing purposes, I'm using postman with a pre request script to create the hash. Everything works fine, with one exception:
In the code tab I have this
however if on the pre request script I dump the value of the request body I get by using request.data I obtain
The problem is, its not exactly the same string, then the value retrieved by request.data creates a hash with a different body that the server uses to create its hash (the server uses the one beautified with line endings and tabs). This is the script where I use the request body content:
So unless someone have an idea of how to retrieve the json body exactly on the format it was written, pretty much seems I cant use postman for this
thanks!
EDIT: I don't know if this existed 5 years ago when the original Question was posted, but it's available in PostMan 8.12.2 at least.
When you have a request open in Postman, show the "Code" pane at the right by clicking the </> button seen in the right-hand toolbar.
Then in the dropdown header for code type, choose "HTTP".
The line numbers in the HTTP snippet are significant -- in my screenshot, the last four displayed lines are concatenated into line 6 of the actual HTTP.
just changed the post body tab to
new post body
and it went just fine. Not really the best solution i was expecting, but does the job :)

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

How can I view responses in Postman Collection Runner?

I am using the Postman Collection Runner to run the same request multiple times using iterations. My tests work as expected, but I'm not able to see the individual responses for each request.
Is it possible to view the responses for requests in the Postman Collection Runner?
In the latest version of Postman you can see all the data from the collection run for each individual request.
In the Collection Runner, Click on the request name and all the details of the request and response can be viewed.
More information can be going on the Debugging using the Request & Response body section of this page
This might help somebody, I tried
pm.test(responseBody, true)
and this printed the response in run summary.
Let me add more details about the shadowcharly solution.
Your results may be viewed if you assign responseBody variable to tests array (in Tests tab)
tests["body"] = responseBody
and export it as json (on the interface you only see the zero/one results if test passed or not).
It's not very useful but this is the only way I've found.
If you are repeating exactly the same query, you could use the test name to show the result:
tests['Test to see value of key Key'+ keyValue] = testResult....
You will have a different test for each value received, not exactly a log, but works fantastic ;)
I know this is old, but postman has added a feature in the collection runner.