Unable to create Token with Cypress E2E

Support Team, I have been trying to create a token credential with the Cypress request method. I was to produce the token within the Postman API library, however, when I try to reproduce the API with Cypress, I keep getting the 401 Error. Below is sample code I use to create the call.

const EMAIL_BASE = 'https://api-sandbox.dwolla.com/token';
  const CLIENT_ID = 'client_id';
  const SECRET = 'secret_key';
  const BASIC_CREDENTIAL = `${CLIENT_ID}:${SECRET}`;
  const CREDENTIAL_ENCODED = Buffer.from(BASIC_CREDENTIAL).toString('base64');

  const formData = new URLSearchParams();
  formData.append('grant_type', CREDENTIAL_ENCODED);

  return cy.request({
    method: 'POST',
    url: EMAIL_BASE,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      'Authorization': `Basic ${CREDENTIAL_ENCODED}`,
    },
    body: formData
  });

@Juan_Navarrete, it looks like the body key for Cypress only allows type object|string and you’re currently sending over an instance of URLSearchParams; however, since this instance has a toString() function, you should be able to call before sending over the string to Cypress’ request function.

For example,

return cy.request({
    method: "POST",
    url: URL_BASE,
    headers: {
        "Authorization": `Basic ${CREDENTIALS_ENCODED}`,
        "Content-Type": "application/x-www-form-urlencoded"
    },
    body: formData.toString()
});

Hope this helps! Please let us know if you have any further questions!

1 Like

Thank you, James, that fixes the issue.

1 Like