How to Extract values for Testscripts from XML Response in postman - postman-testcase

failed to capture the value of name which is "userinfo"
//this what I tried as for eg to extract the name: "userInfo" from xml response
var jsonObject = xml2Json(responseBody);
pm.environment.set("name", jsonObject["methodResponse"]["params"]["param"]["value"]["struct"]["member[0].name"]);
console.log();

Related

MongoDB functions with date query

How does one write a JSON with Data Queries? I am building a mongodb function to accept HTTP request and parse query. I’ve tried new Date(“2016-10-11T00:00:00Z”) and ISO date function. I always end up with JSON parse errors as it can’t accept functions. How can we write a dynamic query string which can be sent in requestbody of http call? Thanks.
{
"query": {"account.companyName":"Groups","_createdAt":{"$gte":ISODate("2016-10-11T00:00:00Z")}},
"projection": {
"crn": 1,
"state": 1
1 }
}
;
t= JSON.parse(body);
var res= collection.find(t.query,t.projection,function(err, cursor){
cursor.toArray(callback);
db.close();
});

Fetch nested data from Internet

How to fetch nested json response ? I can fetch direct value but cant fetch nested values
{
"username":"aa",
"data":{
"key":"value",
"anotherKey":"anotherValue"
}
}
You have to first decode your json like this-
var respBody = jsonDecode(response.body);
Then you can use respBody["data"]["key"]

Usage of GET with request body with GraphQL

I am using REST API GET with payload ( passing input params ) and it returns with JSON response.
I need to convert it into GraphQL, I have the schema and types defined,
but how to merge GET request payload along with GraphQL query JSON and process it to get the desired response back?
Example GET: http://localhost/myproject/getAllCustomers
payload :
{
"name":"Harish"
"passion":"reading"
}
GraphQL query :
{
customer {
name
age
city
}
}
You pass variable to GraphQL query arguments
Query:
query getCustomer($name: String, $passion: String) {
customer(name: $name, passion: $passion) {
name
age
city
}
}
Variable:
{
"name": "Harish",
"passion": "reading"
}

Get all matching items using ids array form database

I can't receive list of items that matches with my array of ids.
This is PART of code in Angular component:
this.orderService.getSpecyficOrders(ids)
.subscribe(orders => { ...
Where ids is an array of
[{_id : ID },{_id : ID },{_id : ID },]
ID is "5235sd23424asd234223sf44" kind of string form MongoDB documents.
In angular service file I have imported:
Http, Headers, and import 'rxjs/add/operator/map';
Here is code in service in Angular:
getSpecyficOrders(ids){
return this.http.get('/api/ordersspecyfic', ids)
.map(res => res.json());
}
In express file I have require: multer, express,router,mongojs, db
And here is part of code in express, call to mongodb:
router.get('/ordersspecyfic', function(req, res, next){
var ids = req.body;
ids = ids.map(function (obj){ return mongojs.ObjectId(obj._id)});
db.orders.find({_id: {$in: ids}}, function(err, orders){
if(err){
res.send(err);
}
res.json(orders);
});
});
And I'm getting error:
Uncaught Response {_body: "TypeError: ids.map is not a function
&n…/node_modules/express/lib/router/index.js:46:12)↵", status:
500, ok: false, statusText: "Internal Server Error", headers:
Headers…}
Console.log in express file
is showing me that req.body is an empty object {}
As far as I know req.body is not an array, but I don't know if this is only problem with that code.
All others request of get single element, get all items etc. are working fine.
I just can't get this one working..
I assume you are trying to send ids to your server side with
return this.http.get('/api/ordersspecyfic', ids)
but http.get api doesn't work like that
get(url: string, options?: RequestOptionsArgs) : Observable
In order to send this data to your back-end you should use the post api
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
return this.http.post('/api/ordersspecyfic', ids, options)
post(url: string, body: any, options?: RequestOptionsArgs) : Observable
Source:https://angular.io/docs/ts/latest/api/http/index/Http-class.html
Two errors, backend and frontend.
Frontend error
You say this.http.get('/api/ordersspecific', ids);. This does nothing - or specifically, this only tries to get /api/ordersspecific. It doesn't send ids, your second parameter doesn't match any RequestOptions. In other words, your ids are ignored.
You'd want to append this as a query string. Check here how to add querystring parameters. But in short, it'd be something simple like:
return this.http.get('/api/ordersspecyfic?ids=<id1>&ids=<id2>...'
Backend error
You're reading stuff from body. It's a GET request, there should be no body. Read this from querystring:
router.get('/ordersspecyfic', function(req, res, next){
var ids = req.query.ids;
});

Non-standard REST API in Backbone js

I am building Backbone.js models backed by a legacy REST API. When I create a new resource on the server, rather than return the JSON of the newly created resource, it returns plain text of the id of the newly created resource, along with a relative URL in the Location header where the resource can be gotten.
For example, I POST:
{ "firstName": "Joe", "lastName": "Blow" }
to http://fakeserver.com/people and (on success) the body of the plain/text response might be: "1234". The status of the response is 201 and the Location header would be http://fakeserver.com/people/1234. If I GET from that Location URL, it will have
{ "id": 1234, "firstName": "Joe", "lastName": "Blow" }
How would I override the sync function on my model to accommodate this convention instead of the default Backbone.js conventions?
To clarify, there is no Backbone.js version of this yet - I am trying to create a new one. The old jQuery-only way of doing it was:
$.ajax({
type: 'POST',
url: submitURL,
data: $.toJSON(person),
success: function(data, status, request) {
showSuccessMessage();
closeDialog();
},
dataType: 'json',
contentType: 'application/json'
});
The details of the showSuccessMessage and closeDialog are unimportant - just to demonstrate that basically we are just ignoring the content of the response and throwing the original object away.
Handle the simple text response with parse:
http://backbonejs.org/#Model-parse
parse : function(response, options){
console.log(response, options);
var data = this.toJSON(): //current model to hash
data.id = response; <- assuming that response is a simple text plain id
return data;
}
You could also use sync to overwrite something in the ajax call(that is not supported in the options hash.
http://backbonejs.org/#Model-sync