I am looking for a way to assert the entire response of my API:
The following error has occurred:
If you make a separate request, to first save the response data to a variable:
let myData = xml2Json(responseBody)
pm.globals.set('myData', JSON.stringify(myData))
You could then try this, in the second request, to check the whole response body against the save variable:
let myResponseData = xml2Json(responseBody)
pm.test('Body is Correct', () => {
pm.expect(myResponseData).to.deep.eql(JSON.parse(pm.globals.get('myData')))
})
Related
How can I validate a response item is an instance of a collection variable where the collection variable is an array in postman?
Here first I'm making an array from a response from a GET request.
let arr = [];
for (item of response.books) {
arr.push(item.isbn);
}
pm.collectionVariables.set("Books_ISBN", arr);
console.log(arr);
Now I want to evaluate a response data of a POST request with the "Books_ISBN" collection variable.
My POST request response is this
{
"books": [
{
"isbn": "9781449325862"
}
]
}
I'm trying to do that like this but it is showing me error.
var response = JSON.parse(responseBody);
pm.test(pm.expect(response.books[0].isbn).to.be.an.instanceof(Books_ISBN));
Postman uses the Chaijs assertion library internally. to.be.an.instanceof checks if the type is an Array. You want to use the oneOf method(Docs) like this:
const Books_ISBN = pm.variables.get("Books_ISBN");
pm.test("my test", () => {
pm.expect(response.books[0].isbn).to.be.oneOf(Books_ISBN);
});
You also maybe want to look at the postman documentation for writing tests and the documentation on how to use variables in scripts.
Save array, object to as a varible, you should stringify first
pm.collectionVariables.set("Books_ISBN", JSON.stringify(arr));
Variable is not existed in script, you have to get it first, don't forget to parse.
let Books_ISBN = JSON.parse(pm.collectionVariables.get("Books_ISBN"));
pm.test("my test", () => {
pm.expect(response.books[0].isbn).to.be.oneOf(Books_ISBN);
});
This is a follow up to my issue at API Pagination in JSON Body (not Header) - How to Access with Power Query in Custom Connector?
I have built a custom connector for an API that sends its pagination data back in the body of the response, rather than the header. The way to use GetNextLink using header is documented in the GitHub custom connector example:
https://github.com/microsoft/DataConnectors/blob/master/samples/Github/github.pq
GetNextLink = (response, optional request) =>
let
// extract the "Link" header if it exists
link = Value.Metadata(response)[Headers][#"Link"]?,
links = Text.Split(link, ","),
splitLinks = List.Transform(links, each Text.Split(Text.Trim(_), ";")),
next = List.Select(splitLinks, each Text.Trim(_{1}) = "rel=""next"""),
first = List.First(next),
removedBrackets = Text.Range(first{0}, 1, Text.Length(first{0}) - 2)
in
try removedBrackets otherwise null;
Getting to the pagination data from the body of the response should be easier than the header, I've learned. But I am stumped. In the images, you can see the record I am trying to access that comes back through Web.Contents()
This is my code for the GetNextLink function. I am trying to get the "next" record above.
GetNextLink = (response) =>
// response is data already run through Web.Contents()
let
Source = Lines.FromBinary(response),
nextPage = Record.Field(Source[paging], "next")
in
try nextPage otherwise null;
I get the error back "We cannot convert a value of type Record to type Text". I feel like I am not getting the 'Source" correctly? I could certainly use help as I am a PowerQuery newbie!
Thanks!
GetNextLink = (response) =>
// response is data already run through Web.Contents()
let Source = Lines.FromBinary(response),
nextpage = try Source[paging][next] otherwise null
in nextpage
or
GetNextLink = (response) =>
// response is data already run through Web.Contents()
let Source = Lines.FromBinary(response)
nextpage = try Record.Field(Source[paging],"next") otherwise null
in nextpage
Let's say my API call URL is www.example.com/quiz with POST method.
And I get the response body like this. And
var jsonData = pm.response.json();
pm.collectionVariables.set("cv_quiz_order", quiz_order)
if(!jsonData.is_end){
// TODO: request next question using `quiz_order`
}else{
// TODO: finish this API and go to the next request.
}
When I use Run Collection. I want it(the Apis) tests one by one in regular sequence. And only this Api repeats until its is_end is true.
How can I do this?
var jsonData = pm.response.json();
pm.collectionVariables.set("cv_quiz_order", quiz_order)
if(!jsonData.is_end){
postman.setNextRequest(pm.info.requestName)
}else{
// TODO: finish this API and go to the next request.
}
this will keep sending the same request until is_end is true
postman.setNextRequest allows you to set the next reqeust to be executed , pm.info.requestName gives thecurrent request Name , so you are saying run this request as next request
I am writing tests collections for endpoints and I want the test to check if the response param estadoAula has the same value as the request param estadoAula so I can test everything went as intended. The param need to be sent in the body and not in the URL
Request Body
{
"estadoAula": "1"
}
Response Body
{
"idAula": "8d4cf346-cda0-47ca-acae-33981738b4b6",
"estadoAula": "1"
}
Test
pm.test("Estado modificado correctamente",function(){
var data = pm.response.json();
let estadoAula = pm.request.body.estadoAula; <--- this doesn´t work, I need to get request param 'estadoAula'
pm.expect(data.estadoAula).to.eql(estadoAula);
});
You'll need to parse the request body, I am assuming you've set it to RAW along with 'JSON' as type.
This script should work for you:
pm.test("Estado modificado correctamente",function(){
let data = pm.response.json(),
requestBody = JSON.parse(pm.request.body.raw);
pm.expect(data.estadoAula).to.eql(requestBody.estadoAula);
});
I have a GET request to OKTA to retrieve some information that uses some variables etc. It returns a body. I have a second request of type PUT where I manually paste the BODY and make a change to one variable. I am trying to determine if I can remove the manual process of pasting in the response body from the 1st GET request onto the second PUT request.
As an example, I have a URL:
GET https://{{myurl}}/api/v1/apps/{{instanceid}}
This returns some dyanmic JSON data in the payload like so
"blah":{ some more blah
},
"signOn": {
"defaultRelayState": null,
"ssoAcsUrlOverride": ""
"audienceOverride": null,
"recipientOverride": null
}
what I am hoping to do is:
PUT https://{{myurl}}/api/v1/apps/{{instanceid}}
{replay entire body from 1st request with the modification of
"ssoAcsUrlOverride": "{{some var that points to a new url}},
}
I have looked at some articles that show:
Using Tests to send a GET request with a static body and replaying that exact body. In this case, I am looking to modify a parameter not replay as=is
I tried this thread here (In postman, how do I take a response body and use it in a new request within Tests
postman-how-do-i-take-a-response-body-and-use-it-in-a-new-request-within-tes) but I get an error stating that responseBody is not defined
First of all, let's validate the JSON response first. Here is the valid JSON with some dummy data.
{
"blah": "some more blah",
"signOn": {
"defaultRelayState": "1",
"ssoAcsUrlOverride": "www.google.com",
"audienceOverride": "true",
"recipientOverride": "yes"
}
}
1) Save first request's response into a environment variable req_body as follows,
var jsonData = pm.response.json();
pm.environment.set("req_body", jsonData);
2) In the PUT request, take another environment variable replace_this_body in body.
3) Get the value of E'variable req_body we had set in the first request in Pre-request script. Then change the value of it and set current request's body variable.
var requestBody = pm.environment.get("req_body");
requestBody.signOn.ssoAcsUrlOverride = "https://www.getpostman.com";
pm.environment.set("replace_this_body", JSON.stringify(requestBody));
Finally, you will get updated request data into PUT request!