Postman Examples Dynamic Response Based on Request Data - postman

Probably missing something completely obvious in the docs but is it possible to echo request data in a Postman Example / Mock Server response based on the input request.
Example Request:
POST:
{
"firstName": "{{$randomFirstName}}",
"lastName": "{{$randomLastName}}",
"phoneNumber": "{{$randomPhoneNumber}}",
"email": "{{$randomExampleEmail}}",
"employeeId": "{{$randomInt}}"
}
Intended example response:
{
"id": {{$randomInt}},
"firstName": "{{$req.firstName}}",
"lastName": "{{$req.lastName}}",
"phoneNumber": "{{$req.phoneNumber}}",
"email": "{{$req.email}}",
"employeeId": "{{$req.employeeId}}"
}

I see you want to use Postman's dynamic 'faker' variables in the request body to be returned in the mock response. A similar use case is supported for the request URL (refer to the section on 'wildcards' here) but not body.
Here's one way to achieve this with the request body:
Create an environment 'e1' with variable 'firstName'.
Edit your mock to add the environment 'e1'.
Use the same environment variable {{firstName}} in the response body of your example.
Dynamically update the value of 'firstName' before sending the mock request. If you're using the Postman client, you can do this using the pm.environment.set method. If not, then you can use the Postman API to do this.
On the other hand, you can simply use the same faker variable {{$randomFirstVariable}} in your mock example response as well, but the value returned could differ from the one sent in the request.

Related

PowerBI fetch data with GET request with body

I am trying to query an API where I have to pass username and password in the body. I have researched multiple methods but have not found any method which seems reasonable.
My query looks something like this:
URL:
localhost:8080/v1/project
HEADERS:
Content-Type: application/json
BODY:
{
"email": "test#test.com",
"password": "123456"
}
In the docs I have found WebActions.Request, which is aparently disabled in most contexts (https://learn.microsoft.com/en-us/powerquery-m/webaction-request).
Web.Contents only allows a body in POST requests (https://learn.microsoft.com/en-us/powerquery-m/web-contents).
I'd rather not write a Python script to do it as it requires a Python installation.
I tried this possible solution, but there was no success, as it would still perform a POST request: https://community.powerbi.com/t5/Power-Query/How-to-send-a-GET-method-with-content-data-with-M/m-p/943033/highlight/true#M32541
What reasonable way is there to achieve my goal to do a GET request with a body containing the credentials?

Validate response in Postman test script

I am inserting data in postman. I want to apply validation from post method in test script. I am new to postman, I am not able to understand.
Here is the JSON.
{
"firstname": "pradeep",
"lastname": "tiwari",
"mobile_no": 9911844349,
"email": "pradeep#gmail.com",
"user_type": 4,
"password": "pradeep1",
"confirmPassword":"pradeep1",
"dob":"nanana",
"u_org":25,
"isfoundingpartner":"undifine",
"gender":1
}
You can use built in snippets for starters to validate your basic response messages. https://learning.getpostman.com/docs/postman/scripts/test_examples/
In order to carry out validation on your responses, you will first need to parse the data into a JavaScript object using below
var jsonData = pm.response.json();
You can use the below code to check for particular values in the response body
pm.test("Verify Json values", function () {
pm.expect(jsonData.firstname).is.to.equal("pradeep");
pm.expect(jsonData.lastname).is.to.equal("tiwari");
pm.expect(jsonData.gender).is.to.equal(1); `
});

API endpoint to save multipart-file by POST method through Postman

I am trying to mock the request to upload a zip file through 'POST' method using Wiremock. But I could not find the required property for that. Following is my mocked request which needs to be sent.
How can I save this file to the _file directory through POST request?
"request":
{
"url": "/order/uploadFile",
"method": "POST",
"headers": {
"token": {
"equalTo": "0000000"
},
"Content-Type":{
"equalTo": "multipart/form-data"
}
},
"bodyPatterns": [{
"equalToJson": "{\"sampleFile\":\"Sample_file.zip\"}"}]
} ....```
Here is the postman request. [![request-postman][1]][1]
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/CQaSj.png
In short: you can't save anything to the __files directory using the out-of-the-box standalone WireMock from withing a mapping rule. This functionality requires a custom extension that needs to do the saving for you.
That said, it is possible, according to this Stack Overflow answer to store a file in __files using a PUT on /__admin/files/[your desired filename]. This will then create a new file under the __files. It appears to be undocumented and as such may not feature in future versions. Sub-folders seem to go unsupported when I tried it.

Postman - Use part of the response data from one test in another test

I require help to execute a postman test which requires a response output from another test. I have checked with various forums but a solution is not available for this particular scenario.
Example
Test 1 response:
{
"items": [
{
"email": "archer+qa01#gmail.com",
"DocumentName": "tc",
"type": "URL",
"url": "https://localhost:8443/user/terms?statusno=a5f2-eq2wd3ee45rrr"
}
]
}
Test 2:
I need to use only the a5f2-eq2wd3ee45rrr part of the response data from Test 1, this can be seen in the url value above. I need to use this value within Test 2
How can I make this work with Postman?
Not completely sure what the response data format is from the question but if it's a simple object with just the url property, you could use something simple like this:
var str = pm.response.json().url
pm.environment.set('value', str.split('=', 2)[1])
This will then set the value you need to a variable, for you to use in the next request using with the {{value}} syntax in a POST request body or by using pm.environment.get('value') in one of the test scripts.
Edit:
If the url property is in an array, you could loop through these and extract the value that way. This would set the variable but if you have more than 1 url property in the array it would set the last one it found.
_.each(pm.response.json(), (arrItem) => {
pm.environment.set('value', arrItem[0].url.split('=', 2)[1])
})
If you get JSON response and then send JSON in body, I would do the following:
1) In test script(javascript):
var JsonBody = pm.response.json();
var strToParse = JsonBody.url;
var value = strToParse.slice(indexOf("?status=")+"?status=".length);//parse string
//manually but you can google for a better solutions.
pm.environment.get("varName" , value)
2) You can use it! In scripts like: pm.environment.get("varName"), and everywhere else using {{varName}}

Why do I get an empty data response?

I've gotten the built in read action to work in the past but now it is not working. I also created a custom action which will not work.
When I try to post the action to this url:
https://graph.facebook.com/me/teamtutorials:complete?access_token=AAACOYImsskcBAGlF33o6awIqxmQ078BVdHUY72CF7GUqTHUhEcpKLdH8ZCKeyQbBqBDlnHwUMwt5aLOzpBmiWQqpvWsNAeHDMPSo2OQZDZD&tutorial=http://teamtutorials.com/web-development-tutorials/why-zen-coding-is-an-awesome-time-saver?
all I get in response is:
{
"data": [
],
"paging": {
"next": "https://graph.facebook.com/me/teamtutorials:complete?access_token=AAACOYImsskcBAGlF33o6awIqxmQ078BVdHUY72CF7GUqTHUhEcpKLdH8ZCKeyQbBqBDlnHwUMwt5aLOzpBmiWQqpvWsNAeHDMPSo2OQZDZD&tutorial=http\u00253A\u00252F\u00252Fteamtutorials.com\u00252Fweb-development-tutorials\u00252Fwhy-zen-coding-is-an-awesome-time-saver\u00253F&offset=25&limit=25"
}
}
What causes this type of response?
That looks like the response you'd get to a HTTP GET request; you need to make a HTTP POST request to create a new action
if your code/proxy can't make POST request outbound or you can fake it by making a GET request and including an extra parameter &method=post