[postman-to-k6] pm.sendRequest

Hi
I have some troubles with the pm.sendRequest. I read thats not supported by the k6 but I have no idea how can I change that part of a code. How can I do that to make it work ? Here it is:

const tokenUrl = 'https://qa-appgw.reykjavik.is/gateway/IdentityServer/1.0/connect/token';
const clientId = 'postman-api';
const clientUser = 'SYS_THJODSKRA_TEST';
const scope = 'thjodskra_api';

const getTokenRequest = {
  method: 'POST',
  url: tokenUrl,
  header:  [
           { key: 'x-Gateway-APIKey', value: pm.collectionVariables.get("QA-IDENTITY-SERVER-KEY") }
   ],
  body: {
      mode: 'formdata',
      formdata: [
          { key: 'grant_type', value: 'client_credentials' },
          { key: 'client_id', value: clientId },
          { key: 'client_secret', value: pm.collectionVariables.get("clientSecret") },
          { key: 'user', value: clientUser },
          { key: 'scope', value: scope }
      ] }
};

pm.sendRequest(getTokenRequest, (err, response) => {
  const jsonResponse = response.json();
  const newAccessToken = jsonResponse.access_token;
  pm.variables.set('access_token', newAccessToken);
});

Hi Jakub, welcome to the forum :slight_smile:

Which version of the converter are you using? I’m not very familiar with how it handles these scenarios. Does everything else work except that you get an error when calling pm.sendRequest()?

Since pm.sendRequest() is unsupported, you can use the k6 http module instead. See the documentation, specifically for http.post() and http.request(). You can use whichever is more comfortable.

For example:

const res = http.request('POST', tokenURL, { grant_type: 'client_credentials', ... },
                         { headers: { 'x-Gateway-APIKey': '...' }});
const accessToken = res.json().access_token;
...

Hope this helps,

Ivan

Hello Ivan

I’m using this command to run the converter: npx @apideck/postman-to-k6 National.json -o k6-script.js
Well I will try to do it your way then.

Jakub

Hey, imiric, I am just curious about one thing, if I use this method then it would be added in K6-Scripts.js manually, is there any workaround to send a request other than pm.sendRequest()? you will write in the postman test and after converting in k6 scripts, additional work will not be needed.