postman

Create unique body request in postman for API-test


I want to create test request on postman with unique email property in the request body.

{
    ...
    "email": "{{email_string}}.com",
    ...
}

I've set email_string with static string in enviroment, but is there any way that I can set email_string dynamically before request occured?


Solution

  • As an alternative to the previous answer, you could use the sendRequest function to get the value from a 3rd party API that is designed to return randomised data.

    This can be added to the Pre-Request Script tab:

    pm.sendRequest("https://randomuser.me/api/", (err, res) => {
        // Get the random value from the response and store it as a variable
        var email = res.json().results[0].email
        // Save the value as an environment variable to use in the body of the request
        pm.environment.set("email_address", JSON.stringify(email))
    })
    

    You could potentially create lots of randomised data using this API but it is a 3rd party API so you won't have any control over this changing. If you only need this in the short term, i'm sure it will be fine.

    Something also worth remembering is that Postman comes with Lodash built-in so that gives you the ability to use any of that modules functions, to reduce down some of the native JS code.