How to Add Headers to Every Request in Cypress
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']
})
})
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:
π More Webtips
Master the Art of Frontend
- Access 100+ interactive lessons
- Unlimited access to hundreds of tutorials
- Prepare for technical interviews