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.

[Question][Bug] How can i add Header on specific routes ?

See original GitHub issue

I want to add custom header like "X-E2E-USER": "fake-user@e2e.com" on header only for specific route (ie: i want to add header for request on my API)

I found this in the doc https://playwright.dev/docs/api/class-route#route-continue that say i can overrides headers.

I try this but it’s doesn’t work. Headers are not overrides.

await page.route(/\/api\//i, async (route, request) => {
    // Override headers
    const existingHeaders = await request.allHeaders();
    const headers = {
      ...existingHeaders,
      "X-E2E-USER": "fake-user@e2e.com", 
    };
    route.continue({ headers });
  });

Maybe i miss something, but it’s not explain in the docs, maybe forget ?

I don’t want to use setExtraHTTPHeaders because it’s override all http call, and i don’t want change all my request

  await page.setExtraHTTPHeaders({ "X-E2E-USER": "fake-user@e2e.com" });

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Horsty80commented, Apr 14, 2022

@aslushnikov yes before is the part is missing for understanding this behavior ^^

Indeed all the mock is working, it’s just the how i check that isn’t good.

Actually my test is good, i get indeed my custom header so i close this issue

Thanks for your answer 😃

0reactions
aslushnikovcommented, Apr 14, 2022

But how i can not see them when i try to log every request ?

@Horsty80 the page.on('request') event happens before the route handler, so you get headers there before you modify them in page.route.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Router: Create header for specific routes - Stack Overflow
I think the best way to do that is to add a Route for each header and pass an array of paths to...
Read more >
Route traffic based on a custom HTTP header using an ...
Select your HTTP listener, and then choose View/edit rules. Choose the Add rules icon (the plus sign), and then choose Insert rule. Choose...
Read more >
Injecting custom headers to http route - Discuss Istio
The headers field can accept only one value of type Headers. How is it possible to add more than one custome header? apiVersion:...
Read more >
Appending and Removing Request/Response Headers
Appending and Removing Request/Response Headers. Gloo Edge can add and remove headers to/from requests and responses. We refer to this feature as “Header...
Read more >
RFC 7230: Hypertext Transfer Protocol (HTTP/1.1)
RFC 7230 HTTP/1.1 Message Syntax and Routing June 2014 ought to be reflected in ... URI, and protocol version (Section 3.1.1), followed by...
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