Using Environment Variables in a request body

My question: I have a request body. In the request body are tokens for variables that I want to get from the environment. These tokens seem to be working fine when used in the URL, but not the body. I’m reading that I must use a pre-request script to make it work, but as I’m not a programmer, I’m having trouble connecting dots.

BODY

{
  "labels": {
    "singular": {{singular-object-name}},
    "plural": {{plural-object-name}}
  },
  "requiredProperties": [
    {{primary-property-internal-name}}
  ],
  "searchableProperties": [
    {{primary-property-internal-name}}
  ],
  "secondaryDisplayProperties": [
    {{secondary-property-internal-name}},
    {{tertiary-property-internal-name}}
  ],
  "properties": [
    {
      "name": {{primary-property-internal-name}},
      "label": {{primary-property-label}},
      "isPrimaryDisplayLabel": true,
      "description": {{primary-property-description}},
      "hasUniqueValue": true
    },
    {
      "name": {{secondary-property-internal-name}},
      "label": {{secondary-property-label}},
      "description": {{secondary-property-description}}
    },
    {
      "name": {{tertiary-property-internal-name}},
      "label": {{tertiary-property-label}},
      "description": {{tertiary-property-description}}
    }
  ],
  "associatedObjects": [
    {{associated-object-one}},
    {{associated-object-two}},
    {{associated-object-three}}
  ],
  "name": {{unique-internal-name}},
  "primaryDisplayProperty": {{primary-property-internal-name}},
  "metaType": "PORTAL_SPECIFIC"
}
}

PRE-REQUEST SCRIPT

pm.environment.get("unique-internal-name");
pm.environment.get("singular-object-name");
pm.environment.get("plural-object-name");
pm.environment.get("primary-property-internal-name");
pm.environment.get("primary-property-label");
pm.environment.get("primary-property-description");
pm.environment.get("secondary-property-internal-name");
pm.environment.get("secondary-property-label");
pm.environment.get("secondary-property-description");
pm.environment.get("tertiary-property-internal-name");
pm.environment.get("tertiary-property-label");
pm.environment.get("tertiary-property-description");
pm.environment.get("associated-object-one");
pm.environment.get("associated-object-two");
pm.environment.get("associated-object-three");

400 BAD REQUEST RESPONSE

{
    "status": "error",
    "message": "Invalid input JSON on line 3, column 22: Unrecognized token 'Work': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')",
    "correlationId": "684f6afd-0863-49b9-a65b-5e1628231529"
}

I have tried: I’ve tried this without the pre-request script, just using the handlebar tokens surrounded by double quotes. That didn’t work. Then I took the double quotes out. That didn’t work. Now I’m trying the pre-request script with and without quotes. Nothing.

Something tells me I have to do more than just get the variables in the script…I also have to set them to the body variables too. But I’m not totally clear how to do that.

Also, I’m wondering if the script is really even needed because isn’t that what the environment variables are for in the first place? Shouldn’t Postman just replace those tokens when the request runs without the need for the script?

Anyway, I completely realize this is a beginner question that has probably been answered a zillion times. I just couldn’t find any answers that focused only on this simple request. They all included arrays and other things which complicate this beyond my own simple ask.

Anyone have 5 minutes to help a new guy?

Hi @spencer

First question, have you remembered to set your environment in the top right hand corner?

image

If you don’t, it won’t grab the variables you have set.

So like this;
Use the left tab to open environments, create an env then add your variables, then on the right, set the environment to be active.

Then you should see the values like this;

Doing this ^^ means that you shouldn’t need a pre-req script too.

Thanks for this.

Yes, I definitely set the environment. And I’ve made sure all environment variables are save.

Based on your comment that I’m correct in thinking a pre-request script is not needed (thank you!), I’m going to go back again and try it without the double quotes. I’m almost sure I did, but this gives me something else to try at least.

1 Like

If you still hit issues, copy in your console output for the request body.

Oh for Pete’s sake!!

I’m so frustrated with myself. I thought I had saved environment variables because each time I opened it, they were there. Obviously, that’s not the case.

Per your suggestion, I looked at the request body and noted that nothing was pulling in and realized the problem.

I’m a dope. Thanks for your help.

2 Likes

Can you use env variables in a GraphQL body?

I have created this short video to show running example of adding variable to json body.

Hope it helps someone.