How to Add Headers to Every Request in Cypress

How to Add Headers to Every Request in Cypress

Ferenc Almasi β€’ 2022 January 10 β€’ Read time 1 min read
  • twitter
  • facebook
Copied to clipboard! Playground
beforeEach(() => {
    cy.intercept(`${Cypress.config('baseUrl')}**`, req => {
        req.headers['Auth'] = Cypress.env('token')

        // or to delete a header
        delete req.headers['Id']
    })
})
support/index.js

You can use the above code to add extra headers to every request you make in Cypress. Make sure you place the file in your support folder, inside a beforeEach hook.

It's a good practice to use a baseUrl, which you can access using Cypress.config. Using the asterisk wildcard, you can match every path under the domain.

Note that requests are intercepted at proxy level, therefore additional headers won’t show up in the Network tab.

Want to learn Cypress from end to end? Check out my Cypress course on Educative where I cover everything:

Learn Cypress with Educative
How to Add Headers to Every Request in Cypress
  If you would like to see more webtips, follow @flowforfrank

  • twitter
  • facebook
Did you find this page helpful?
πŸ“š More Webtips
Frontend Course Dashboard
Master the Art of Frontend
  • check Access 100+ interactive lessons
  • check Unlimited access to hundreds of tutorials
  • check Prepare for technical interviews
Become a Pro

Courses

Recommended

This site uses cookies We use cookies to understand visitors and create a better experience for you. By clicking on "Accept", you accept its use. To find out more, please see our privacy policy.