Search code examples
javascriptpostmanchai

Postman variable into string


I feel kinda dumb asking this question bcs I might be missing something super easy but.. I have a collection of requests in Postman.

I have a collection variable test_model_id which is 888.

What I'm trying to achieve is simply putting the model_id variable into the string in resp_data line. I feel like I tried everything lol.

var model_id = parseInt(pm.collectionVariables.get("test_model_id"));

pm.test("Test_response_model_creation", function () {
    var jsonData = pm.response.text();
    var resp_data = JSON.stringify({"detail":"Model {{model_id}} created","HTTPStatusCode":200});
    pm.expect(jsonData).to.eql(resp_data);
});

Getting

... to deeply equal '{"detail":"Model {{model_id}} created","HTTPStatusCode":200}'

Solution

  • Have you tried using template laterals?

    var resp_data = JSON.stringify({"detail":`Model ${model_id}` created","HTTPStatusCode":200});