[postman-to-k6] Postman variables in body won't get replaced

Hi,

I recently started looking for load testing tools other than JMeter. I came across the postman-to-k6 converter, and it’s a great addition. But I’m finding a problem, mainly in the variables inside the body are not being replaced. The code looks as follows (removed great part of it to not make this too long):

// Auto-generated by the Load Impact converter

import "./libs/shim/core.js";
import "./libs/shim/expect.js";
import { group } from "k6";

export let options = { maxRedirects: 4 };

const Request = Symbol.for("request");

postman[Symbol.for("initial")]({
  options,
  environment: {
    baseurldevices: "http://localhost:5000/api/v1/device",
    baseurlservices: "http://localhost:5000/api/v1/service",
    access_token: "1eb317717b627d476f3e08fa13773270a71d1a37",
    ulServiceName: "ULServicePostman",
    ulEntityType: "UTest",
    ulApiKey: "UL0000",
    ulAttributes: '[{\n      "object_id": "1",\n      "name": "temp",\n      "type": "string"\n    },\n      {\n        "object_id": "2",\n        "name": "humidity",\n        "type": "string"\n      }\n    ]\n',
    ulCommands: "[]",
    ulStaticAttributes: "[]",
    ulThingProtocol: "ul",
    ulPublic: "true",
  }
});

group("services", function () {
  postman[Request](
    {
      name: "Add UL service",
      id: "7a579799-67ed-4bf0-a7ab-22ebbffe825e",
      method: "POST",
      address: "http://" + __ENV.baseurlservices + "/add",
      data:
        //'{\n    "name": "ulServiceName",\n    "entityType": "ulEntityType",\n    "thingProtocol": "ul",\n    "apiKey": "ulApiKey",\n    "commands": [],\n    "attributes": [],\n    "staticAttributes": [],\n    "public": true \n}',
        '{\n    "name": "{{ulServiceName}}",\n    "entityType": "{{ulEntityType}}",\n    "thingProtocol": "{{ulThingProtocol}}",\n    "apiKey": "{{ulApiKey}}",\n    "commands": {{ulCommands}},\n    "attributes": {{ulAttributes}},\n    "staticAttributes": {{ulStaticAttributes}},\n    "public": {{ulPublic}}\n}',
      headers: {
        "Content-Type": "application/json",
        "x-nick-name": "{{access_token}}"
      },
      post(response) {
        pm.test("Status code is 201 Created", function () {
          pm.response.to.have.status(201);
        });
        pm.test("Response body contains the expected values", function () {
          pm.expect(pm.response.text()).to.include(pm.environment.get("ulServiceName"));
          pm.expect(pm.response.text()).to.include(pm.environment.get("ulEntityType"));
          pm.expect(pm.response.text()).to.include(pm.environment.get("ulApiKey"));
          pm.expect(pm.response.text()).to.include(pm.environment.get("ulThingProtocol"));
          pm.expect(pm.response.text()).to.include(pm.environment.get("ulPublic"));
        });
        var service = JSON.parse(responseBody);
        pm.environment.set("ul_Id", service._id);
      }
    })
});

The problem comes in

data:
  //'{\n    "name": "ulServiceName",\n    "entityType": "ulEntityType",\n    "thingProtocol": "ul",\n    "apiKey": "ulApiKey",\n    "commands": [],\n    "attributes": [],\n    "staticAttributes": [],\n    "public": true \n}',
  '{\n    "name": "{{ulServiceName}}",\n    "entityType": "{{ulEntityType}}",\n    "thingProtocol": "{{ulThingProtocol}}",\n    "apiKey": "{{ulApiKey}}",\n    "commands": {{ulCommands}},\n    "attributes": {{ulAttributes}},\n    "staticAttributes": {{ulStaticAttributes}},\n    "public": {{ulPublic}}\n}',

The different variables are not being replaced. If I use the top option (the one that is commented), it works, but for the bottom option, it says errors because it finds "{" in the parameter.

I tried making console.log("{{ulServiceName}}") and it prints exactly that ("{{ulServiceName}}") instead of the value.

How can I make use of those variables? it does work inside the headers tag and the address using the same structure.

Thanks.

This should have been fixed with https://github.com/loadimpact/postman-to-k6/issues/12 .
Can you check you are running the latest version and if this still persist please open a new issue with the postman-to-k6 project.

1 Like

Uninstalled the version I had, downloaded the github project, made “sudo npm install -g” from the root of the downloaded project, then executed:
postman-to-k6 /home/myuser/Descargas/device-management.postman_collection.json -e /home/myuser/Descargas/device-management_environment.postman_environment.json -o /home/myuser/Descargas/k6-script_new.js

Executed the script with version 0.24.0 of k6, but the problem seems to be the same:

[1] Unexpected token { in JSON at position 164,
[1] SyntaxError: Unexpected token { in JSON at position 164

The output is basically the same I had before downloading and installing from github. I suppose my flow was correct, or am I missing something? just to be sure before opening the issue.

What you are doing is suppose to work, so you should open an issue.

1 Like

I had the same issue and it was resolved by replacing the line 935 of libs/shim/core.js, ie.

config.data = data

to:

config.data = evaluate(data)
2 Likes

Looks like this works. I’ll open an issue anyway so it can be fixed for everyone. Thanks.

1 Like

Sorry for unburying such an old thread, but given how many views this topic has it can’t really be helpt.

:warning: Do not do this

Running npm install with sudo should not be necessary. The same goes for global installation using -g.

Doing so might lead to other problems, compromising security being the most prominent one.

If this is actually needed, something else is wrong with your node configuration.

1 Like