Error accessing request.headers middleware
See original GitHub issueVerify canary release
- I verified that the issue exists in the latest Next.js canary release
Provide environment information
Operating System: Platform: win32 Arch: x64 Version: Windows 10 Pro Binaries: Node: 16.13.2 npm: N/A Yarn: N/A pnpm: N/A Relevant packages: next: 12.2.5 eslint-config-next: 12.2.5 react: 18.2.0 react-dom: 18.2.0
What browser are you using? (if relevant)
Chrome
How are you deploying your application? (if relevant)
No response
Describe the Bug
I tried to access the request.headers in the Next.js Middleware but the data is not showing up.
If I access something else, the data can appear
if i display headers it gets an error
Server Error
TypeError: Cannot delete property 'Symbol(set-cookie)' of #<HeadersList2>
- Img 1 = Code in Middleware
- Img 2 = Fill in the data in the request variable
- Img 3 = Error when I display
request.headers
Expected Behavior
i can access request.headers.referer
Link to reproduction
To Reproduce
Issue Analytics
- State:
- Created a year ago
- Reactions:10
- Comments:5
Top Results From Across the Web
Error accessing request.headers in Next.js middleware
You can't access the headers object directly inside the middleware. If you want to access a specific header you should use the get...
Read more >Using custom HTTP Headers · Twirp
Read HTTP Headers from requests · Write some middleware (a func(http.Handler) http.Handler) that reads the header's value and stores it in the request...
Read more >Cross-Origin Resource Sharing (CORS) - MDN Web Docs
The Access-Control-Request-Headers header notifies the server that when the actual request is sent, it will do so with X-PINGOTHER and Content- ...
Read more >Request and response objects - Django documentation
HttpRequest.headers is a simpler way to access all HTTP-prefixed headers, plus CONTENT_LENGTH and CONTENT_TYPE . HttpRequest. headers ¶. A case insensitive, ...
Read more >Headers - Traefik Labs documentation
In Traefik Proxy, the HTTP headers middleware manages the headers of requests ... Regular expressions and replacements can be tested using online tools...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I had the same problem and seems like accessing directly
request.headers
object throws that type of error, which is counter intuitive at all, and hard to debug for FE person, maybe a specific error handling for that case would be a good idea?The solution is to ether access needed header by
request.headers.get('header-name-here')
. If you need to list them, you may userequest.headers.values(callback)
. Thank should solve the issueThis closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.