I have Get request which gives me response like below
{
"var1": "value1",
"var2": "value2"
}
I am saving it in an environment variable from Tests script as below
postman.setEnvironmentVariable("allData", JSON.stringify(responseBody));
In next Post request, I am trying to retrieve above values from Pre-request script as below
var jsonData = JSON.parse(allData)
However I am getting not defined error as below
There was an error in evaluating the Pre-request Script:
ReferenceError: allData is not defined
I can set each property in an individual variable and that works fine but that pollutes environment (as there are around 20 such properties). Please suggest better alternate for the same. Also suggest me how to access individual values in Body of the Post request. Can I do something like below?
{
"var1": "{{jsonData.var1}}",
"var2": "{{jsonData.var2}}"
}
OR I need to set values to individual variable in Pre-request script and use them in Body?
Thanks
If need to retrieve the data from the saved variable as a whole data set, you would need to make this reference to it when declaring the variable:
var jsonData = JSON.parse(pm.environment.get("allData"))
If you want to be able to use the single values from your variable in the Request Body you would need to parse them individually, in the Pre-Request Script, then store them as variables to use in the Request Body:
pm.environment.set("my_single_var_1", JSON.parse(pm.environment.get('allData')).var1)
pm.environment.set("my_single_var_2", JSON.parse(pm.environment.get('allData')).var2)
From here you can then set the {{my_single_var_1}} syntax in the Request Body and these placeholders would resolve to the values you have set.
Related
PostMan supports faker-library and provides several variables like "randomFirstName" and so on. However, I try to make them work in a pre-request script.
I've found a blogpost about this topic telling that it actually should work:
pm.variables.replaceIn("{{$randomProductName}}")
However, for me it doesn't work. The variable won't be replaced:
var firstName = "{{$randomFirstName}}"
console.log(firstName)
The output is:
{{$randomFirstName}}
How can I use these variables in a pre-request-script in PostMan?
You can't use the {{}} syntax in the pre-request and test tabs.
The following will do:
var firstName = pm.variables.replaceIn("{{$randomFirstName}}");
Blockquote
Hi All,
I have a question. I just started using Postman, so please sorry if I'm not using all the correct technical terms.
I want to have a Envirionment variable in postman that contains several XML tags.
F.e.
<Storess>
<Store>
<Id>322</Id>
</Store>
<Store>
<Id>323</Id>
</Store>
<Store>
<Id>324</Id>
</Store>
</Storess>
I want everything between the tags <Storess> and </Storess> to be copied to the environment variable so that I can use {{Storess}} in the next request that i'm sending.
How should I do this?
This may be useful: https://www.marklogic.com/blog/postman-test-scripting/
In the test script of the first request, you can set the result in an environment variable like this:
var result = pm.response.text();
console.log(result);
result = result.replace("<Storess>", "").replace("</Storess>", "").trim();
postman.setEnvironmentVariable("Storess", result);
console.log(postman.getEnvironmentVariable("Storess"));
You will be able to get the information in the next request like this:
{{Storess}}
If you want the XML inside the variable Storess to be well-formatted, you'll need to work it out a little bit.
I have created a comment model and trying to fetch all comments records. But I need a meta info total comments which is getting as a separate attribute outside comments array.
I am using Ember store.query to fetch records from rest service(I tried store.findAll, but it is giving me only record array of comments in promise response. Is it possible to modify that?). I am getting the records with total comments(meta) while using store.query(), but that record array is not getting updated when we save new records.
After doing some analysis I found that we can use filter for loading the live record, but filter is now deprecated in Ember(Ember 2.5.1). From the documentation It is clear that we can use ember-data-filter for loading live record. But I am confused to use that addon(Mentioned like it has some memory leakage issue) and not sure whether I will get meta information from response. Is there is any other way to fetch live records with meta information from the response.
Anyone please suggest a solution
After doing some analysis, I found a solution to access meta data using store.findAll(). We can use typeMapFor in the findAll response to get the meta info in the response
store.typeMapFor(response.type)
Full code below,
store.findAll("comment").then(function(response) {
var meta = store.typeMapFor(response.type);
// your meta info will be in meta.metadata
// var totalComments = meta.metadata.totalComments;
});
And the response record array is liveRecords which will get updated automatically, if we save new records.
store.query("comment").then(function(response) {
var meta = response.get("meta");
// We will get meta like this but reponse record array is not a liveRecords
});
Response getting from store.query() is just a recordArray (not liveRecords) which will not get updated with new records
If you want an array of all records that updates as new records are populated you can use peekAll which returns a live record array.
Added Code sample:
loadRecords: function (){
this.set('allComments', store.peekAll('comment'));
this.store.findAll('comment');
},
recordCount: Ember.computed.alias('allComments.length')
I am trying to make a test in Postman to verify some content in a JSON response. If I just try to verify a single line from the JSON response everything is fine. My problem starts when I need to test multiple lines of the JSON response. Is always failing. Any suggestion?
tests["Body matches string"] = responseBody.has("\"name\": null,
\"nameType\": \"NON_REFUNDABLE\"");
If I understand your question correctly I'd like to suggest that you approach this in a different way.
Instead of looking at the entire response body and seeing if the strings match you could alternatively test the individual Json properties that make up the response body. For example you could do the following:
var data = JSON.parse(responseBody);
tests["name is null"] = data.name === null;
tests["nameType is non-refundable"] = data.nameType === "NON_REFUNDABLE";
There are other alternatives as well but this is the first that comes to mind. For some more ideas about testing using postman check out their documentation and examples.
am getting the following post data in my django app
POST
Variable Value
csrfmiddlewaretoken u'LHM3nkrrrrrrrrrrrrrrrrrrrrrrrrrdd'
id u'{"docs":[],"dr":1, "id":4, "name":"Group", "proj":"/al/p1/proj/2/", "resource":"/al/p1/dgroup/4/","route":"group", "parent":null'
am trying to get the id value in variable id i.e "id":4 (the value 4). When I do request.POST.get('id')I get the whole json string. u'{"docs":[],"dr":1, "id":4, "name":"Group", "proj":"/al/p1/proj/2/", "resource":"/al/p1/dgroup/4/","route":"group", "parent":null' How can I get the "id" in the string?
The data you are sending is simply a json string.
You have to parse that string before you can access data within it. For this you can use Python's json module (it should be available if you're using Python 2.7).
import json
data = json.loads( request.POST.get('id') )
id = data["id"]
If you somehow don't have the json module, you can get the simplejson module.
For more details, refer this question : best way to deal with JSON in django
That's happening because id is string, not dict as it should be. Please provide your template and view code to find source of problem.