headers
The headers function allows you to read the HTTP incoming request headers from a Server Component.
headers()
This API extends the Web Headers API. It is read-only, meaning you cannot set / delete the outgoing request headers.
Good to know
headers()is a Dynamic Function whose returned values cannot be known ahead of time. Using it in a layout or page will opt a route into dynamic rendering at request time.
API Reference
Parameters
headers does not take any parameters.
Returns
headers returns a read-only Web Headers object.
Headers.entries(): Returns aniteratorallowing to go through all key/value pairs contained in this object.Headers.forEach(): Executes a provided function once for each key/value pair in thisHeadersobject.Headers.get(): Returns aStringsequence of all the values of a header within aHeadersobject with a given name.Headers.has(): Returns a boolean stating whether aHeadersobject contains a certain header.Headers.keys(): Returns aniteratorallowing you to go through all keys of the key/value pairs contained in this object.Headers.values(): Returns aniteratorallowing you to go through all values of the key/value pairs contained in this object.
Examples
Usage with Data Fetching
headers() can be used in combination with Suspense for Data Fetching.
IP Address
headers() can be used to get the IP address of the client.
In addition to x-forwarded-for, headers() can also read:
x-real-ipx-forwarded-hostx-forwarded-portx-forwarded-proto
Version History
| Version | Changes |
|---|---|
v13.0.0 | headers introduced. |
Last updated