Postman Printing specific information from Response - postman

I'm using Postman's console to display the response of the API Call with console.log, I'm using the runner since I have a lot of iterations. However, a lot of information from the API response are giving me trouble, so I would like to do is to print with console.log specific information of the responseBody.
As test with Postman, I'm using the following:
var body = JSON.parse(responseBody);
console.log(JSON.stringify(body.data));
The response is:
[{"device":"1BED7","time":1505320342,"data":"05b006bcac00000000000000","snr":"21.00","linkQuality":"AVERAGE","seqNumber":555,"rinfos":[{"tap":"A2A","delay":1.4,"lat":"53.0","lng":"2.0"},{"tap":"A2B","delay":0.5,"lat":"53.0","lng":"2.0"}]},{"device":"1CED7","time":1505277142,"data":"05b006bcac00000000000000","snr":"20.68","linkQuality":"AVERAGE","seqNumber":554,"rinfos":[{"tap":"A2C","delay":1.3,"lat":"53.0","lng":"2.0"},{"tap":"232","delay":1.9,"lat":"53.0","lng":"2.0"}]},{"device":"152C3","time":1505233937,"data":"05b006bcac00000000000000","snr":"19.14","linkQuality":"AVERAGE","seqNumber":553,"rinfos":[{"tap":"215","delay":2.4,"lat":"53.0","lng":"2.0"}]},{"device":"1BF81","time":1505190735,"data":"05b006bcac00000000000000","snr":"21.67","linkQuality":"AVERAGE","seqNumber":552,"rinfos":[{"tap":"1CC","delay":2.0,"lat":"53.0","lng":"2.0"},{"tap":"25A","delay":1.6,"lat":"53.0","lng":"2.0"}]},
What I would want to print with console.log would be only the values of device, time and data:
{"1BED7",1505320342,"05b006bcac00000000000000"},{"1CED7",1505277142,"05b006bcac00000000000000"},{"152C3",1505233937,"05b006bcac00000000000000"},
and so on...
My programming skills are very limited so sorry if the answer is so obvious, I have tested a lot of things but I'm still stuck.
Thanks a lot if you can help

I think your your response is array of objects.It is already a json object.So firstly what you are doing wrong is you don't need to parse it.You can directly use it.Check this below code snippet at the end of the answer.I think this satisfies your need.I used the forEach function to iterate through the the response array and push value that you need into the empty array.This result array contains object in following format.you can access each property of each object of this array by javascript . operator.I think that is quite obvious to you.
[
{
"device": "1BED7",
"time": 1505320342,
"data": "05b006bcac00000000000000"
},
{
"device": "1CED7",
"time": 1505277142,
"data": "05b006bcac00000000000000"
},
{
"device": "152C3",
"time": 1505233937,
"data": "05b006bcac00000000000000"
},
{
"device": "1BF81",
"time": 1505190735,
"data": "05b006bcac00000000000000"
}
]
var responseBody=[{"device":"1BED7","time":1505320342,"data":"05b006bcac00000000000000","snr":"21.00","linkQuality":"AVERAGE","seqNumber":555,"rinfos":[{"tap":"A2A","delay":1.4,"lat":"53.0","lng":"2.0"},{"tap":"A2B","delay":0.5,"lat":"53.0","lng":"2.0"}]},
{"device":"1CED7","time":1505277142,"data":"05b006bcac00000000000000","snr":"20.68","linkQuality":"AVERAGE","seqNumber":554,"rinfos":[{"tap":"A2C","delay":1.3,"lat":"53.0","lng":"2.0"},{"tap":"232","delay":1.9,"lat":"53.0","lng":"2.0"}]},{"device":"152C3","time":1505233937,"data":"05b006bcac00000000000000","snr":"19.14","linkQuality":"AVERAGE","seqNumber":553,"rinfos":[{"tap":"215","delay":2.4,"lat":"53.0","lng":"2.0"}]},{"device":"1BF81","time":1505190735,"data":"05b006bcac00000000000000","snr":"21.67","linkQuality":"AVERAGE","seqNumber":552,"rinfos":[{"tap":"1CC","delay":2.0,"lat":"53.0","lng":"2.0"},{"tap":"25A","delay":1.6,"lat":"53.0","lng":"2.0"}]}];
var array=[];
responseBody.forEach(function (obj) {
array.push({device:obj.device,time:obj.time,data:obj.data})
})
console.log(array);

let results = _.map(JSON.parse(responseBody),
(sensor) => { return [sensor.device, sensor.time, sensor.data]});
// results contains an array like
// [[deviceId1, time1, data1], [deviceId1, time1, data1], ...]
console.log(results);

Related

Postman Variables

I am trying to parse "Id" from below json to store it in a variable
{
"cf35864e-944d-11e9-8aff-22000ab8d684": {
"Id": "1a45b704-944e-11e9-8aff-22000ab8d684",
"Name": "Test plan",
"Type": "Limited",
"Credits": 10119.70,
"AvailableCredits": 500.100
}
}
I've tried
bodydata =JSON.parse(responseBody)
planid=bodydata.cf35864e-944d-11e9-8aff-22000ab8d684.Id
console.log(planid)
But postman throws errors, is there any way so that only ID can be fetched.
This should work: planid=bodydata["cf35864e-944d-11e9-8aff-22000ab8d684"]['Id']
As per your mentioned JSON data, the below may work.
let resp = pm.response.json();
console.log(resp.cf35864e-944d-11e9-8aff-22000ab8d684.Id);
Above will work only if "cf35864e-944d-11e9-8aff-22000ab8d684" value is static and only one object is returned.

Tests and Environments

i'm newbie here and newbie also using postman.
I'm trying to use environments to store values from responses and use them to the next request. I found some examples on the web and i use them.
I managed to store the first value in a environment but not the 2nd, 3rd, in the same request. I tried many different ways to write the tests but without success
My tests' code is this:
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("clientID", jsonData.LoginClientID);
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("COMPANY", jsonData.objs.data[0].LoginCompan
Response was this:
{
"success": true,
"clientID": "abcd",
"objs": [
{
"COMPANY": "1",
"BRANCH": "1",
"MODULE": "0",
}
],
"ver": "5.00.518.11143"
}
Running the POST request the value of clientID is stored in enviroment's value but not COMPANY
Any advice ?
Thanks Eddie
Just remove the data part of the code when setting the variable. You're just after the list of items in that objs array, not sure where data comes into it.
For example, from your code:
jsonData.objs[0].LoginCompan
More information about extracting values from a response body can be found here:
https://community.getpostman.com/t/sharing-tips-and-tricks-with-others-in-the-postman-community/5123/5

Posts likes works on Graph API Explorer but on the server returns empty array

I've been playing with Facebook Graph API a little bit and everything works just fine, I can get the feed of my page, the comments, but have a problem with likes.
It turns out that when I make an api call from Graph API Explorer like this:
me/posts?fields=likes.summary(true)
I get the correct response:
{
"likes": {
"data": [
],
"summary": {
"total_count": 0,
"can_like": true,
"has_liked": false
}
},
"id": "000000000_00000000"
},
ID is edited for this example. However when I make this call from server, I just get an empty array.
Does anybody know, what's the matter and how to solve this? Thanks
Okay, it turns out that the method "toArray()" made the likes array empty. I'll find another way to convert the GraphEdge to Array.

Best JSON array representation c++

Before i have this json body:
{
"users": [{
"userId": "userId1"
},
{
"userId": "userId2"
}
]
}
This body is send by my java client (its not matter for this case) and i receive in my c++ server with one list.I iterate the list and get all user id from request body.
List example: std::list<UserId>* pUsers;
But now i want to improve and make more easy my body request, so i want an body request like this:
{
"users": {
"userId": ["userId1", "userId2"]
}
}
Note: I only put two users but it can be many of users. I'm new with c++ so i have some difficulties. What is the best representation to do this on c++? Its is one list too? Its a vector? Its a array of strings? Someone can help me and show me some examples or explain to me the best solution?
Thanks.

Passing JSON data from response to request in Django

I have a Django (1.8.3) view that:
Makes a GET request to Server A (jetty), which returns JSON data in the body of the response. Then,
Makes a POST to Server B (node.js), passing the JSON data recieved from Server A in the body of the request.
The JSON data is structured like:
{
name: "foo",
details: {
"date": "today",
"isCool": "no",
},
stuff: [
{
"id": "1234",
"rating": "5",
}, {
"id": "5678",
"rating": "1",
},
]
}
But I can't figure out how to get the JSON from Server A's response into the request to Server B in my Django view. If I do this:
jetty_response = requests.request(method='GET', url=jetty_url)
node_response = requests.request(method="POST", url=node_url,
data=jetty_response.json())
I get the JSON object in Server B, but it looks like this:
{
name: "foo",
details: [ "date", "isCool"],
stuff: [ "id", "rating", "id", "rating"]
i.e. the name property is correct, but the details dict is instead received as the keyset of the original dict, and the stuff list is received as a flat array of the keysets in all objects in the original dict.
If I instead do this in django:
node_response = requests.request(method="POST", url=node_url,
data=json.dumps(jetty_response.json()))
I get an empty object in node, and same goes if I do simply:
data=jetty_response.content
How do I make this request??
Figured it out myself.
As is usually the case, the simplest answer:
node_response = requests.request(method="POST", url=node_url,
data=jetty_response.content)
worked fine once I took a closer look at my log and realized my POSTs were bouncing back 413, and then adjusted the size limit on my bodyParser in express.