Parsing Error - Unexpected token formdata

Hi,

Can anyone please with K6 IO - Getting parsing error for a property used which has special characters as below? How can we solve that please? causing issue

  form_key: val,
  login[username]: 'test153@mailinator.com',
  login[password]: 'Testing123',

image

Cannot save this property as a variable as during the POST request, it does not work.

Thanks
Youven

Hi,

I’m not exactly sure what you’re trying to do, but if you want to define object keys dynamically, you need to surround them with square brackets. For example:

let formdata = {
    form_key: val,
    [login[username]]: 'test153@mailinator.com',
    [login[password]]: 'Testing123',
    send: '',
};

This assumes there’s a login object with a 'login[password]' key, which seems strange to me, but hope this helps. :slight_smile:

1 Like

Hi @Youven,

In the absence of additional details, I’ll have a go at this question from two different angles.

  1. If you are indeed trying to set the key to the string login[username] which I for sure would discourage, if you have any say at all into how this is implemented, you could do:
let formData = {
  key: 'test',
  'login[user]': 'hello'
};

// -> {key: "test", login[user]: "hello"}
  1. If you, however, are looking to add a login object to your form data that contains the properties username and password, this would be the idiomatic approach:
let formdata = {
    form_key: val,
    login: {
      username: 'test153@mailinator.com',
      password]: 'Testing123',
    },
    send: '',
};

as you otherwise, which @imiric also stated in his reply, would get this behavior instead:

let user = 'johnny'
let login = {
 johnny: 'helloKey'
};

let formData = {
  key: 'test',
  [login[user]]: 'hello'
}; // -> {key: "test", helloKey: "hello"}

Thanks,
Simme

1 Like

Hi Imiric/Simme,

Thanks both for feedback.

I managed to solve the issue - Using solution 1 above works fine.

Thanks again both for help
Youven

1 Like