-In post call, entered the following in pre-request script
postman.setGlobalVariable('Date1', (new Date()).toISOString());
-From the get call response, getting Date2
-I need to validate whether Date2 > Date1. it's in UTC format "2021-04-22T14:34:00.262Z"
How to do this in Postman? Any help is appreciated
let moment = require('moment')
pm.expect(moment("2021-04-23T02:22:53.610Z").isAfter("2019-04-23T02:22:53.610Z")).to.be.true
pm.expect(moment("2021-04-23T02:22:53.610Z").isBefore("2019-04-23T02:22:53.610Z")).to.be.true
https://momentjs.com/docs/ is supported in postman , you could use that .
Related
I've got a PATCH request that looks like this:
{{host}}/api/invoice/12345678/withdraw
host is a variable determining the environment.
For this request I need to add a unique authorization token.
The problem is I need to send dozens of such requests. Two things change for each request:
id of invoice (for this case is '12345678')
auth token (herebetoken1).
How can I automate it?
You can use Postman Runner for your problem. In Runner, you can send specified requests in specified iterations and delay with data (json or csv file).
For more info, I suggest you take a look at the links below.
Importing Data Files in Postman
Using CSV and JSON Data Files
Request:
Runner:
Data: (You can choose one of them)
Json Data: (data.json)
csv Data: (data.csv)
Preview Data in Runner:
Result:
use the below pre-request script , and call replace id in url and auth in authorization with {{id}} and {{token}} variables . Use collection runner to execute it .
Replace the hashmap with what you requires
hashmap = {
"1234": "authtoken1",
"2222": "authtoken2"
}
pm.variables.get("count") === undefined ? pm.variables.set("count", 0) : null
let keyval = Object.entries(hashmap)
let count = pm.variables.get("count")
if (count < keyval.length) {
pm.variables.set("id", keyval[pm.variables.get("count")][0])
pm.variables.set("token", keyval[pm.variables.get("count")][1])
pm.variables.set("count", ++count)
keyval.length===count? null:postman.setNextRequest(pm.info.requestName)
}
Example collection:
https://www.getpostman.com/collections/43deac65a6de60ac46b3 , click inport and import by link
I have 2 requests
1st Request
After did my first request, I get the response where I can parse for a taskId
In my test tab, I will then parse and store it like this
let taskId = pm.response.json().body.result[0].data.task
console.log(taskId)
I can see taskId printing in my console as 938
2nd Request
I require making a GET with this dynamic URL with the taskId that I got from the first one
http://localhost:3000/fortinet/monitor/{{taskId}}
So I set the above URL , set the HTTP verb to GET
in my Pre-request Script tab, I did this
let taskId = pm.globals.get("taskId")
Result
ReferenceError: taskId is not defined
Image Result
How can I debug this further?
The most suggested way is to use :key as in
http://localhost:3000/fortinet/monitor/:taskId
See the colon before taskId. The reason being, URI values sometimes many not be environment dependent. So, based on the usecase, you can use like I said or {{taskId}}
You have to set variable, but you are doing it wrong.
try this:
pm.globals.set("taskID", pm.response.json().body.result[0].data.task)
more you can read here:
https://learning.postman.com/docs/postman/variables-and-environments/variables/
Please note, that URL which ends with resource identified like https://example.com/:pathVariable.xml or https://example.com/:pathVariable.json will not work.
You can go with https://example.com/:pathVariable with Accept: application/json header.
For passing dynamic value, first you have to set it in environment or global variable in Tests tab because tests runs after request and you will get response value after request sent, but because you get response in json you have to first parse it, so what you can write in Tests tab is as follows:
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("taskId", jsonData.token); // OR
postman.setGlobalVariable("taskId", jsonData.token);
Then you can use taskId as {{taskId}} wherever you want in url parameters or in request body or form data wherever.
If you want to know in detail how to extract data from response and chain it to request then you can go to this postman's official blog post which is written by Abhinav Asthana CEO and Co Founder of Postman Company.
I have got order information from soap Mangeto api. all information I get correct but I get created_at, updated_at date field wrong.
I get created_at value 2014-02-18 11:36:06 but it will be 2014-02-18 17:06:06.
I have been finding the answer to spent several hours. Magento API returns all date values in GMT. I have set Timezone = India Standard Time (Asia/Calcutta) (GTM 5.30+) in admin . website got change but web service are still returning GMT time.
anyone have idea how to get date format in given timezone in soap web service.
$proxy = new SoapClient( SOAP_CLIENT_URL );
$sessionId = $proxy->login( SOAP_CLIENT_USERNAME, SOAP_CLIENT_PASSWORD );
$result = $proxy->call($sessionId, 'order.list',array(
array('customer_id'=>2)));
Can we set any param in order web service? or Magento has any other way to solve the problem.
I'm making the following request through command-line cURL:
curl -X POST http://localhost:8000/api/places/ -vvvv -d "place[name]=Starbucks"
However, when I try to access the parameters by calling
request.POST.getlist('place')
I get an empty array as a response. How can I access the sub-dictionary which I can then pass to the ORM?
Thanks,
Jamie
HTTP data elements can't have sub-elements. The data you have posted - as shown in the querydict - has been interpreted as a single element with key "place[name]" and value "Starbucks". So you can get it with request.POST["place[name]"].
It looks like you are sending a string, in that case try:
request.POST.get('place[name]')
If your are simulating a dropdown list you should send "place=Starbucks", however if you are trying to send an array you should try to convert you string to an array inside your python script.
In your command you can get ride of "-X POST" as the parameter -d is already an HTTP POST:
curl --help
...
-d/--data <data> HTTP POST data (H)
curl manual:
http://curl.haxx.se/docs/manual.html
All,
Although I see related topics on the forum, but I don't see a clear solution on this issue.
I am trying to set a javax.servlet.http.Cookie with an expiration time (so that it persists across browser sessions). Code:
public void respond(HttpServletRequest req, HttpServletResponse resp) {
int expiration = 3600;
Cookie cookie = new Cookie("TestCookie", "xyz");
cookie.setDomain("");
cookie.setVersion(0);
cookie.setPath("/");
cookie.setMaxAge(expiration);
cookie.setSecure(false);
resp.addCookie(cookie);
}
I don't see this cookie being set when I check in IE developer tools. Searching on the internet gave me clues that IE doesn't consider Max-Age, but only works with Expires. If this does not work for IE, then is there a proven way of setting the HTTP response headers for a persistent cookie so that it works for IE?
PS: This works fine on all other browsers.
I tried creating a string for the cookie having expires attribute. IE succeeded in creating it, but it lost the domain (default - "") and showed ".com" and turned it into a session cookie instead of a persistent cookie. This again works fine on all other browsers.
Please help.
Thanks.
Working with IE9, I found that it was the HttpOnly attribute that was required in order to get it to echo the cookie value on subsequent posts, e.g:
Set-Cookie: autologCk1=ABCD; Path=/autolog/; HttpOnly
The answer is at Persistent cookies from a servlet in IE.
Your case may be a different flavour of the same issue: that is, by prefixing the domain with a "." (which I'm pretty sure is a version 1 cookie feature), something in the Java stack decides it's a version 1 cookie (unrecognized and not persisted by IE, even IE8) and sends that cookie format.
Or, as that answer suggests, is there something in your cookie value that contains an unrecognized character?
As javax.servlet.http.Cookie does not allow you to set Expires attribute to the cookie, you should set it manually.
You also need to know that Expires must be specified in the form of Wdy, DD Mon YYYY HH:MM:SS GMT following RFC-2616 Full Date section (more info).
In Java you can do it this way:
public void respond(HttpServletRequest req, HttpServletResponse resp) {
int expiration = 3600;
StringBuilder cookie = new StringBuilder("TestCookie=xyz; ");
DateFormat df = new SimpleDateFormat("EEE, dd-MMM-yyyy HH:mm:ss 'GMT'", Locale.US);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 3600);
cookie.append("Expires=" + df.format(cal.getTime()) + "; ");
cookie.append("Domain=; ");
cookie.append("Version=0; ");
cookie.append("Path=/; ");
cookie.append("Max-Age=" + expiration + "; ");
cookie.append("Secure; ");
resp.setHeader("Set-Cookie", cookie.toString());
}