[postman-to-k6] pm.sendRequest

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,


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.