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?