I am trying to find a way to use data from my .json file in the pre-request script in Postman.
As you can see in the code below I am using a loop for my request.
In my pre-request script i would like to use "id_group":"14803,14805" from my file Data.json instead of id_group = ["14803","14805"];.
Thanks
URL:
.../group/{{id_of_group}}/members
Body:
{
"id_user": {{id_user}}
}
Pre-request script:
var id_group = pm.environment.get("id_group");
if (!id_group) {
id_group = ["14803","14805"];
}
var currentIdGroup = id_group.shift();
pm.environment.set("id_of_group", currentIdGroup);
pm.environment.set("id_group", id_group);
Tests:
var id_group = pm.environment.get("id_group");
console.log(id_group);
if (id_group && id_group.length > 0) {
postman.setNextRequest('Add user to groups');
} else {
postman.setNextRequest();
}
Data.json file:
[{
"id_user":47091,
"id_group":"14803,14805"
}]
You are creating an Array-Object. But the pm.environment.set() stores only strings.
You must convert them into strings with JSON.stringify().
Instead of pm.environment.set("id_of_group", currentIdGroup); i would suggest
pm.environment.set("id_of_group", JSON.stringify(currentIdGroup));
And backwards the same. If you are loading from the env vars, you have to parse your stringified objects:
JSON.parse(pm.environment.get("id_group"));
I have just found an answer.
In the pre-request script I use the variable id_group, this variable is used to get the id's which are going to be used in the loop.
I found pm.iterationData.get();, it will take the data from the JSON file. Instead of id_group = ["14803","14805"]; I use pm.iterationData("id_group").
My pre request script look like this now:
var id_group = pm.environment.get("id_group");
if (!id_group) {
id_group = pm.iterationData.get("id_group");
}
var currentIdGroup = id_group.shift();
pm.environment.set("id_of_group", currentIdGroup);
pm.environment.set("id_group", id_group);
And I cheat a little, my JSON look like this now:
[{
"id_user":47091,
"id_group":["14803","14805"]
}]
Related
Imagine if I want to wait to get JSON file If they post to me. I can get it. if it's not I waite for it. I want to get it and store it and parse it to work on it.
(I need to receive the request )How I can have JSON file I can get it to take what I need then post my HTTP response to the agent that sends me file that file.
my JSON file name is : flow_demo_1
var device_array = { device_id : [["7547092d-e03e-5148-a54f-8276a85d7f70","cpp_node_192-168-56-1:6111"]] };
var from_file_array = [
{ device_id_from_file: "7547092d-e03e-5148-a54f-8276a85d7f70" },
{ device_id_from_file: "93e71ba7-fb56-5592-a5f6-d855203dd7ae" },
{ device_id_from_file: "93e71ba7-fb56-5592-a5f6-d855203dd7ae" },
{ device_id_from_file: "fe21abdf-706f-5c7b-adb8-2507e145e820" },
];
var val = device_array['device_id'][0][0];
for (var i= 0; i < from_file_array.length; i++) {
if(from_file_array[i].device_id_from_file === val){
console.log("device ids are matches");
};
};
try this,you just want the which file data is matched right?
from_file_array.filter(each=>each.device_id_from_file===device_array.device_id[0][0])
it will give the matched device_id_from_file;if not you will get empty array
I'm working with protractor and I'm trying to implement data driven.
I'm using this code that I found searching in google.
This is my test.json
{
"username":"testusername"
}
And i'm calling this json from my spec and trying to print in console:
var fs = require('fs');
var content = fs.readFileSync('test/e2e/tests/test.json');
console.log(content.username);
The test is not failing but in the console is print undefined
enter image description here
I'm missing something? or i'm not implementing well the json?
Hope you can help me.
You just need to do require(json_file_path). It will work. Try below code.
var content = require('test/e2e/tests/test.json');
console.log(content.username);
You are not parsing the JSON. readFileSync does read the file as a string. So content is a string containing the JSON, not an Object.
The solution is to simple use JSON.parse:
var content = JSON.parse(fs.readFileSync('test/e2e/tests/test.json'));
Try this solution.
On Main spec:
var testData = require('./Data.json');
describe('...', function() {
testData.forEach( function (data) {
var strUser = data.strUser;
var strPass = data.strPass;
it('...', function() {
<code here>
});
});
});
On Data.json (which is located on same folder as Main spec):
[{
"strUser": "user1",
"strPass": "pass1"
},
{
"strUser": "user2",
"strPass": "pass2"
},
{
"strUser": "user3",
"strPass": "pass3"
}]
Hope it helps :)
I'm receiving some JSON data from a get request and I need to format this data into a table and then write it to an excel sheet. What is the best way to do this?
How about a tab separated csv file?
function makeCsv(table) {
var contents = [];
table.forEach(function(row) {
contents.push('"'+row.join('";"')+'"');
});
return new Blob(contents, { type: 'text/csv' });
}
window.location = URL.createObjectURL(makeCsv([[1, 2],[3, 4]]));
you could use HTML5 saveAs, see FileSaver
I am new to JavaScript and I'm trying to read data from a file and save it in list so that I can retrieve it later. Something like:
sample.json
{
"circles": [
{"x":14,"y":2,"z":4,"r":50},
{"x":14,"y":2,"z":4,"r":50}
]
}
What I am looking for is:
var circle_list = []
circle_lst = some_function_which_reads_json_from_file('sample.json')
//Now circle list contains
circle_list = [
{"x":14,"y":2,"z":4,"r":50},
{"x":14,"y":2,"z":4,"r":50}
]
Later I can just do something like:
for (var i = 0; i <circle_list.lenght;i++){
//do osmething
}
I was looking into
$.getJSON("sample.json" , function(data){
//
});
But then I came to know this call is asynchronous... But I need to maintain execution order.
Don't maintain execution order. This will lock up the browser during the fetching. Use this only as an absolute last resort:
var circle_list;
$.ajax({'dataType': 'json', url: 'sample.json', 'async': false})
.done(function(json) {
circle_list = json;
});
Instead, you can do at least a couple of other things:
Work with callback. It doesn't have to all be in one spot thanks to deferreds.
Load the JSON along with the rest of the page and fetch it from the DOM.
I have a JAVASCRIPT array that looks like this:
postarray['min_price'] = 120000;
postarray['max_price'] = 150000;
I'm trying to pass this to an AJAX call via jQuery .post function so that the .PHP file gets it in this format:
$_REQUEST['query']['min_price'] = 120000;
$_REQUEST['query']['max_price'] = 150000;
So far I've tried:
$.post("ajax_findproperties.php", {query: postarray},
function(data){
// processing function with JSON result
}
,'json');
But I've had no luck. I even tried changing the var postarray to query and then tried query.serialize() in place of the bracketed variable block, but with no luck either.
When I check my status on Firebug, the AJAX call has absolutely no POST vars set whatsoever - complete blank.
The javascript array is not an array, it's an object. Define it before:
var postarray = {};
postarray['min_price'] = 120000;
postarray['max_price'] = 150000;
or replace with:
var postarray = {
min_price: 120000,
max_price: 150000
};
Now the JSON.stringify works:
alert(JSON.stringify(postarray));
Also see this example.
But this object should also be send without JSON.stringify():
$.post("ajax_findproperties.php", {query: postarray}, ... );
Have you tried converting it with JSON.stringify(); and then doing a json_decode(...); in the PHP script?
Try this solution : add [] to your query key
$.post("ajax_findproperties.php", { 'query[]': postarray },
function(data) { },
'json');
Source : http://api.jquery.com/jQuery.post/#example-2