I have postman set up to post data to an endpoint. Ex:
{
"gender": "f",
"firstName": "bob",
"lastName": "smith"
}
I have a test that extracts information from the response and stores it in environment variables.
Is there a way to access the values that were posted as part of the test. Something like:
pm.request.parameters("gender")
As per the postman sandbox documentation, you can do the following:
var reqBody = JSON.parse(pm.request.body);
... // Access reqBody;
To get access to that specific property:
JSON.parse(pm.request.body.raw).gender
And the test could be:
pm.test("Check request data", () => {
pm.expect(JSON.parse(pm.request.body.raw).gender).to.eql('f')
})
Related
I have a schema validation test in my postman collection, which validates if the response adhere to the schema. This is how I do it.
var schema =
{
"type": "object",
"properties": {
"data": {
"type": "object",
"properties": {....
}
pm.test("Schema Validation - TC001", function(){
pm.response.to.have.jsonSchema(schema);
});
When I execute just this script, it validates the schema of the response successfully.
However, in my postman collection I have declared a global function, prior to the schema validation, using Object.prototype() and I'm calling the function as _.funcABC("a","b","c")
Object.prototype.funcABC = function (var1, var2, var3) {
console.log("test");
}
And, my schema validation fails, when I run the entire collection.
While troubleshooting, I came across this, which indicates that the Object.prototype could interfere with JSONschema.
Is there a way to overcome this interference of Object.prototype() on JSONschema? So far, I couldn't find a workable solution.
Thanks.
What stops you from doing this:
pm.test('validate schema', function () {
let temp = Object.prototype.function1
delete Object.prototype.function1
pm.expect(ajv.validate(schema_response, response)).to.true;
Object.prototype.function1 = temp
})
I am working on a POSTMAN collection. Say, I have two separate postman environments with each having URL variables, lets domain1 & domain2. In my initial script in pre-request tab I want to get a list of all the environments available so I can switch them when I need to. How do I get the list of environments?
Thanks,
Thanks Christian Bauman. I was able to accomplish by doing following
In postman Pre-request Script tab. The response will contain environment array with object having id, name, owner, uid properties. you can then call by id to get further details of an environment.
let options = {
method: 'GET',
url: 'https://api.getpostman.com/environments',
header: {
'x-api-key': 'PMAK-your own key goes here'
},
json: true
};
let envs = [];
pm.sendRequest(options, function(err, response) {
if (!err) {
let data = response.json();
_.forEach(data.environments, function(item) {
envs.push(item);
});
console.log(envs);
} else {
console.log(err);
}
});
It is not possible to select environment from scripts. the closest one can get, is to receive the name of the currently active environment: pm.environment.name
I pass the below params in the request body:
{
"locale": "en",
"type": "abc",
"model": {
"id": "123"
}
}
in raw JSON format.
I am trying to retrieve the same params in the test tab using requestBody= JSON.parse(pm.request.body.raw); but then I get a NaN when I write the output to the console.
How could I retrieve the params sent in body request to my test scripts?
You can use this in the Tests tab to log any value from that request body:
console.log(JSON.parse(pm.request.body.raw).locale)
console.log(JSON.parse(pm.request.body.raw).type)
console.log(JSON.parse(pm.request.body.raw).model)
console.log(JSON.parse(pm.request.body.raw).model.id)
I have a problem Working in Api manager of the Anypoint platform at https://anypoint.mulesoft.com/apiplatform..
I made an API Raml definition and am testing all endpoints using the Mock service. So when making a call to the Api endpoint a mock baseurl is provided and the response consists of the example provided with the called endpoint / http verb. This works fine for GET but when doing a 'Try It' for the POST I get
status 400 error
{
"error": "body: person: required"
}
as a response. No matter how I provide the body parameters. My endpoint POST definition is:
post:
body:
application/json:
properties:
person:
required: true
type: object
token:
required: true
type: string
example: |
{
person: {
"firstName": "John",
"infix": "",
"id": "605a0302-cc33-449a-ac50-5ef26e3e3330",
"emailaddress": "john#doe.nl",
"lastName": "Doe"
},
token: '42E2BC51-6C62-6D46-AC1457446EC4C737'
}
In the Api workbench' Mocking service pane I enter this in the body:
{
person: {
"firstName": "John",
"infix": "",
"id": "605a0302-cc33-449a-ac50-5ef26e3e3330",
"emailaddress": "john#doe.nl",
"lastName": "Doe"
},
token: '42E2BC51-6C62-6D46-AC1457446EC4C737'
}
but I still get "error": "body: person: required" also if I omit token ....
What am |I doing wrong here???
Ok., turns out devil's in the details. Input was bad formed json - should surround keys with "" and no singleqoutes for the token value... :(
the json format defined in the raml is not proper one, can you change the raml exmple to a valid json format and test it. it will work fine
Sometimes, we need to modify the response JSON data before it be sent to client. for example:
//model definition
{
"name": "File",
"base": "PersistedModel",
"properties": {
"filename": {
"type": "string",
"required": true
},
"filepath": {
"type": "string"
}
}
"protected": ["filepath"]
}
I want to get a url property on GET /files/:id request, and so I defined a url GETTER on prototype.
//file.js
module.exports = function(File){
var baseUrl = 'http://example.com/uploads/files/';
File.prototype.__defineGetter__('url', function(){
return baseUrl + this.id.toString() + this.filename;
});
}
My question is How to expose the url property to remote response when I make a request as following?
GET /files/123456
expect a response like:
{
id: '123456',
filename: 'myfile.ext',
url: 'http://example.com/uploads/files/123456/myfile.ext'
}
Thanks a lot!
Use a remote method/hook and customize your response accordingly. See https://github.com/strongloop/loopback-example-app-logic/blob/master/common/models/car.js.
You can use Operation Hooks to intercept CRUD actions independently of the specific method that invoke them.
The code below will add the url property to the File object when loading a File object.
File.observe('loaded', function(ctx, next) {
var baseUrl = 'http://example.com/uploads/files/';
ctx.data.url = baseUrl + data.id + data.filename;
next();
});
This will get called when any of the methods below are invoked, either directly in your JS or indirectly via the HTTP API.
find()
findOne()
findById()
exists()
count()
create()
upsert() (same as updateOrCreate())
findOrCreate()
prototype.save()
prototype.updateAttributes()
Other Operation Hooks include:
access
before save
after save
before delete
after delete
loaded
persist