Reading values from the "Body" and setting environment variables

Hi,

I am trying to ready values from the BODY and a json request (no the response) and then set it in a variable. I can successfully do it the response body but not the request body.

Its a post request

Body:

{
  "PolicyDN": "\\VED\\Policy\\Demo\\APIs\\Certificates",
  "ObjectName": "api-example.se.venafi.com",
  "Subject": "api-example.se.venafi.com",
  "SubjectAltNames": [
        {
      "TypeName": "DNS",
      "Name": "venafisan-example.se.venafi.com"
        },
        {
      "TypeName": "DNS",
      "Name": "venafisan-example2.se.venafi.com"
        }
    ]
}

Tests Tab

tests[""] = !(responseBody.has(“Error”));
var data = JSON.parse(responseBody);
postman.setEnvironmentVariable(“Cert_DN”, data.CertificateDN.replace(/\/g,"\$&"));
postman.setEnvironmentVariable(“GUID”, data.Guid);

Tyring to add something for the "Request Value in the request body. Something like this (This does not work)

var mydata = JSON.parse(requestBody);
postman.setEnvironmentVariable(“Subject”, mydata.Subject);

I am sure the solution/syntax is easy. Just not sure how to do it.

Any help would be greatly appreciated.

Hey @Jmeldrum

Are you trying to set a variable from the request body? I’m unsure of the reason for doing this.

Would you be able to explain your use case a little bit more to help provide context and allow us to get to a solution quicker?

1 Like

In the test script, you can parse request body by the following way:

let requestData = JSON.parse(pm.request.body);
pm.globals.set("username", requestData.username);

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.