question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Default headers for cy.request command

See original GitHub issue

Current behavior:

There is no possibility to add default headers for the cy.request command. The only way to add such headers is to overwrite this command

Desired behavior:

The possibility to set default headers (and maybe some other default options) for the cy.request command. For example, we use SessionId header for authenticated requests in our app. And I would like to implement custom login command for my tests, so it would set this header as default for all subsequent requests.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:18
  • Comments:14 (1 by maintainers)

github_iconTop GitHub Comments

23reactions
mattvb91commented, Aug 28, 2018

👍 for this. Would like to also have the option to globally set headers for all cy.visit() requests.

9reactions
Qwalcommented, Nov 30, 2018

As a workaround I overwrite cy.request method with my custom command:

Cypress.Commands.overwrite('request', (originalFn, ...options) => {
  const optionsObject = options[0];

  if (optionsObject === Object(optionsObject)) {
    optionsObject.headers = {
      authorization: 'Bearer YOUR_TOKEN',
      ...optionsObject.headers,
    };

    return originalFn(optionsObject);
  }

  return originalFn(...options);
});

Please note it only works if you use cy.request with options object.

Read more comments on GitHub >

github_iconTop Results From Across the Web

request - Cypress Documentation
A request body to be sent in the request. Cypress sets the Accepts request header and serializes the response body by the encoding...
Read more >
Network Requests | Cypress examples (v7.1.0) - Gleb Bahmutov
To make an XHR request, use the cy.request() command. // https://on.cypress.io/request ... From the response, you can get the individual headers.
Read more >
How can I set my request headers in Cypress to avoid ...
When I set the request headers in options section Cypress ignores them and replaces with default ones. The example of my request:
Read more >
How to Add Headers to Every Request in Cypress - Webtips
You can use cy.intercept in a beforeEach hook inside your support files to add extra headers to every request you make in Cypress....
Read more >
Request - Cypress - W3cubDocs
Cypress sets the Accepts request header and serializes the response body by ... Pass in an options object to change the default behavior...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found