Dynamic Variable in Request Body Validateion Error

Hi,

I am using a dynamic variable {{$randomInt}} in my request body and the API Designer’s schema validation on for the collection is indicating that there are validation errors.

I am using the following request body in my collections request:

{
	"email": "{{email_prefix}}shipper{{$randomInt}}{{$randomInt}}{{$randomInt}}-postmantest{{email_suffix}}",
	"password": "{{PASSWORD}}",
	"firstName": "{{$randomFirstName}}",
	"lastName": "{{$randomLastName}}",
	"phoneNumber": "{{phonenumber}}",
	"companyNumber": {{$randomInt}},
	"companyName": "{{$randomFullName}}",
	"address": "{{$randomStreetAddress}}",
	"city": "Winnipeg",
	"state": "MB",
	"zipCode": "R3C0A5",
	"country": "ca",
	"operateInUS": false,
	"dotNumber": "{{$randomInt}}"
}

When I validate the schema for the collection’s request, I end up with the following validation changes suggested.

If I change to not have the {{$randomInt}} in my request body but hard code an integer value, I do not receive any validation errors.

{
	"email": "{{email_prefix}}shipper{{$randomInt}}{{$randomInt}}{{$randomInt}}-postmantest{{email_suffix}}",
	"password": "{{PASSWORD}}",
	"firstName": "{{$randomFirstName}}",
	"lastName": "{{$randomLastName}}",
	"phoneNumber": "{{phonenumber}}",
	"companyNumber": 2,
	"companyName": "{{$randomFullName}}",
	"address": "{{$randomStreetAddress}}",
	"city": "Winnipeg",
	"state": "MB",
	"zipCode": "R3C0A5",
	"country": "ca",
	"operateInUS": false,
	"dotNumber": "{{$randomInt}}"
}

2 Likes

Thanks for your post.
You can create a payload variable in your pre-request script, stringify that Json variable and use it in the body of your request.
For instance the variables email_prefix and the variable password could be set in your pre-request script. Inside the pre-request script you can create a Json object and put on those variables. Afterwards, you can JSON.stringify(payload) and use the variable in the body of your request like this: {{payload}}.
There are some tutorials online regarding using dynamic variables in Postman such as Postman: The Complete Guide - REST API Testing along with the postman documentation.
I hope it helps :slightly_smiling_face:

An example of a request body with a payload variable: