How to get value from response body by using environment variable - api

This is my response body, I want to get the value of status by using an environment variable.
{
"success":[
{
"code":"200",
"message":"Success",
"details":"Station was retrieved successfully."
}
]
}
i can get value of status code as: jsonData.success.code,
But instead of this, I'm doing it like,
I m setting an environment variable named 'sh' n I gave value as "code"...
pm.environment.set("sh","code");
var s = pm.environment.get("sh")
jsonData.status.success[0].s
By doing like this I'm not able to get the path...Any solutions?
Thanks in advance.

It's hard to tell what your actual response body is but this should probably work:
let statusCode = pm.response.json().success[0].code
pm.environment.set("statusCode", statusCode)
Ensure that you create an environment file before running the request.

Related

What is the variable type for {"message":"ok"} in terraform

I'm writing a terraform module for GCP uptime checks and I need to filter {"message":"ok"} in response body.
Need to pass {"message":"ok"} as a variable, but still cannot find the suitable variable type for it.
I tried complex variable types. But issue not resolved yet
I think you can use a map as variable, example :
variable.tf file :
variable "message" {
description = "Message"
type = map
default = {
"message" = "ok"
}
}
Display the message value in the outputs.tf file for example :
output "message_value" {
value = var.message["message"]
}
Thanks all This is resolved by using jsonencode({ "message" : "ok" })

Is there a way for postman to read the response data from a GET request and then use an IF THEN statement to run a POST request?

I am trying to run a script where postman sends a Get request, and if the get response contains a certain variable to then run a Post request. How do i do this?
(also is there a way to run a get request hourly)
In pre-requisite add:
// set initial value
const method = pm.variables.get("method")
// set initial value as GET if method is undefined
method ? null : pm.variables.set("method", "GET")
// Set this as method
pm.request.method =method
in test script add :
// the condition check
if (pm.response.json().somevalue === "somevalue") {
//then change the method
pm.variables.set("method", "POST")
//call the same request again using setNExtRequest
// pm.info.reqeustName gives current request's name
postman.setNextRequest(pm.info.requestName)
}

How to Set Variable from Request in Postman

I am trying to write some tests for Postman. Many of the requests require an API key that gets returned by an initial GET request.
To set something hard-coded that is not dynamic, it looks like the test code is of the form
let variable = pm.iterationData.get("variable");
console.log("Variable will be set to", variable);
How do I set a return value as a global variable and then set that as a header parameter?
#Example if you setting ApiToken that is dynamic.
Under Tests tab in postman.
Put the following code.
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("Token", jsonData.token);
Under specific environment put variable name as "Token" and current value will be set automatically.
access the variable using {{variable_name}}
#Example: {{token}}
You can specify that variable value in the request Headers by using the {{var_name}} syntax. Instead of any hardcoded value that you may have been using.
You would previously have had to set the value using the pm.globals.set()syntax.
If you do not have the SDK active (monthly payment required to Postman). You can retrieve the values through __environment and __globals.
//Use of environment
postman.setEnvironmentVariable("my_value", "This is a value");
//...
let myValueVar = postman.__environment["my_value"];
console.log("Variable value is:", myValueVar);
//shows: Variable value is: This is a value
//Use of Globals
postman.setGlobalVariable("my_value2", "This is a value of globals");
//...
let myValueVar2 = postman.__globals["my_value2"];
console.log("Variable value is:", myValueVar2);
//shows: Variable value is: This is a value of globals
Also you can see your globals variables in Environments->Globals of your Posmant client.

Set response headers

I need to set some response headers within server.onPreHandler ext method.
There are 2 scenarios, where I need this happen when user sends an API request to my route end point.
1) In success scenario, I need to set headers and let process continue further down life cycle
2) In error scenario (where user has not provided a required field), I need to set headers and return immediately to user with appropriate error info.
In both of these scenarios, I would like to set response headers.
In the 2nd scenario above, I am able to invoke reply.response('error') and then set response header to it using response.header('x', 'value'). However, in the 1st scenario, where before calling reply.continue() I am trying to set header using request.response.header('x', 'value), I get response null error.
Please help
Thanks
Ramesh
I'm able to change response headers like this. Have you tried this way?
// at your onPreResponse ext body
const response = request.response;
if (request.response.isBoom) {
response.output.headers['x'] = 'value';
} else {
response.header('x', 'value');
}

POSTMAN: Get Generated Request in test to compare to Response

I am using some of the auto generated parameters in my request body in a postman request(i.e: {{$guid}}).
I would like in my test to retrieve the request that was sent to the server to compare what this variable value was, and what the response parroted back to me me in my request.
for example, my request's body looks like this:
{
"Description": "testing this {{$guid}}"
}
and I would in the tests be able to do:
var req = JSON.parse(requestBody);
var resp = JSON.parse(responseBody);
test['description should match'] = req.Description === resp.Description;
is this doable?
This is possible.
But you have several small syntax errors.
To access the request body data use:
var req = JSON.parse(request.data);
I named the variable req to not be confused with the predefined request variable. You can log the result like this:
console.log(req.Description);
In the tests tab make sure you reference the correct variable tests with "s". Also you pass the test case name as a string e.g. "description should match".
var res = JSON.parse(responseBody);
console.log(res.Description);
tests["description should match"] = req.Description === res.Description;