I have a scenario where i need to pass the body element values to environmen variable and access it in another API.
Below is the body,
{
"firstName" : "James",
"lastName" : "Joseph",
"email" : "{{timestamp}}#test.com",
"password" : "{{timestamp}}",
"country" : 16
}
Below is the Pre-req script,
postman.setEnvironmentVariable("timestamp", (new
Date).getTime());
// I have copied the Body and paste it in a variable called Obj in Pre-
req
// Then i used the below script to get the body
pm.environment.set("rawBody", JSON.stringify(obj));
But the environmental values of timestamp , email and password is coming as below. The timestamp value is correct and other two are wrong.
timestamp = 1566076106769
email = {{timestamp}}#test.com
password = {{timestamp}}
The timestamp value is not getting substituted in email and password,i want the environmental variable value to set as,
email = 1566076106769#test.com
password = 1566076106769
So how can i assign the body element value to an environment/global variable to use in another API call?
Related
I'm trying to send a message to an SQS queue. I have everything setup correctly.
I'm using a fifo queue, so my post string looks like this:
https://queuename?Action=SendMessage&MessageBody=TEST&MessageGroupId=6&MessageDeduplicationId=6
The above works and the body of the message is TEST, However, I'd like to send data in JSON format
In the body tab, I have my payload formatted in JSON. How do I get that JSON value into the MessageBody field as a variable?
Step 1. Save json in a variable
const body = {
"key": "value"
}
//encoded the special character to make it valid in URL
const payload = encodeURIComponent(JSON.stringify(body))
//Put it in an environment variable
pm.environment.set("payload", payload)
Step 2: Use this var in URL
https://queuename?Action=SendMessage&MessageBody={{payload}}&MessageGroupId=6&MessageDeduplicationId=6
I have request with number of tests cases, same endpoint, different actual values, different expected error messages.
I would like to create parameterized request sending particular value and check particular error message from list with all of the cases.
Request body:
{
"username": "{{username}}",
"password": "{{password}}",
...
}
Response:
{
"error_message": "{{error_message}}",
"error_code": "{{error_code}}"
}
Error message changes due to different cases:
Missed username
Missed password
Incorrect password or username
etc
Now, I have separate request on each case.
Question:
Is there way have 1 request with set of different values, checking
particular error messages/codes?
Create a csv:
username,password,error_message,error_code
username1,password1,errormessage1,errorcode1
username1,password1,errormessage1,errorcode1
Now use this as data file in collection runner or newman.
variable name is same as the column name and, for each iteration you will have corresponding row-column value as the variable value. Eg for iteration1 username will be username1
. As danny mentioned postman has a really rich documentation that you can make use of
https://learning.postman.com/docs/running-collections/working-with-data-files/
Adding another answer on how to run data driven from same request:
Create a environment variable called "csv" and copy the below content and paste it as value:
username,password,error_message,error_code
username1,password1,errormessage1,errorcode1
username1,password1,errormessage1,errorcode1
Now in pr-request add :
if (!pm.variables.get("index")) {
const parse = require('csv-parse/lib/sync')
//Environmental variable where we copy-pasted the csv content
const input = pm.environment.get("csv");
const records = parse(input, {
columns: true,
skip_empty_lines: true
})
pm.variables.set("index", 0)
pm.variables.set("records", records)
}
records = pm.variables.get("records")
index = pm.variables.get("index")
if (index !== records.length) {
for (let i of Object.entries(records[index])) {
pm.variables.set(i[0], i[1])
}
pm.variables.set("index", ++index)
pm.variables.get("index")===records.length?null:postman.setNextRequest(pm.info.requestName)
}
Now you can run data driven for that one particular request:
Eg collection:
https://www.getpostman.com/collections/eb144d613b7becb22482
use the same data as environment variable content , now run the collection using collection runner or newman
Output
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!
In a postman request json body, I want to set a variable to a variable.
This works fine for the built in $timestamp, But can I do it for other numerics?
It seems that postman always sends the variable as a string encoding.
For Example, in the pre-request script:
var lat = +pm.environment.get('lat');
lat = clat + 0.001;
console.log(lat);
pm.environment.set('lat', Number.parseFloat(lat).toPrecision(5));
In the body:
{
"unixTimeStamp": {{$timestamp}}, //OK
"lng": 145.123, // OK
"lat": {{$lat}}, // Fails
}
This sends:
{
"unixTimeStamp": 1521769729,
"lng": 145.123,
"lat": "-37", // Web service rejects this field.
}
I believe that numeric is always encoded as a string.
The $ in front of the variable name is only used In a few places for the built in dynamic variables. For example $timestamp, $guid and $randomInt.
Every value in the Environment file is stored as a string so you would need to parse this in some way to get the value that you require.
You can use parseInt(pm.environment.get('my_string') to convert a saved string "100" into the number 100 in the request or maybe parseFloat depending on what you need.
Then in the request body use
"lat": {{lat}},
If a PUT request is sent to a URI such as http://foo.com/api/employees/123 where '123' is the employee ID, but the message body looks like this:
{
"id" : 444,
"firstname" : "John",
"lastname" : "Doe"
}
How should the service handle it?
Is the expectation to update employee '123' to now become employee '444' (effectively moving the resource) or should it return an HTTP error response (and if so, what should it be?)
I think that the id within the URI should be used and no id present in the message body. You have duplicated hints here.
The content you provide within the PUT method contains the data you want to update and it's not the case of the identifier.
Here is how I would adapt your call:
PUT http://foo.com/api/employees/123
(some headers like Content-Type: application/json)
{
"firstname" : "John",
"lastname" : "Doe"
}
Perhaps this link could help you: http://templth.wordpress.com/2014/12/15/designing-a-web-api/.
Hope it helps.
Thierry
You might use this Id according what you want to do into your put resource. Usually, id in resource uri is used to get item by id from persistence db. You can get item by uri id then you can set this id into body, but this will be odd!