How to Add Headers to Every Request in Cypress
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:
Rocket Launch Your Career
Speed up your learning progress with our mentorship program. Join as a mentee to unlock the full potential of Webtips and get a personalized learning experience by experts to master the following frontend technologies: