Intercept externals URLs via Nextjs middleware #70102
Replies: 3 comments 2 replies
-
Are you trying to do a proxy? Have you tried using a export async function GET(request: Request, context: { params: Params }) {
...
fetch("https://example.com/", {
"Authorization": "my-auth-token",
});
...
} |
Beta Was this translation helpful? Give feedback.
1 reply
-
|
In the middleware you can perform a rewrite to your external API adding new headers: Add headers: return NextResponse.rewrite(
'https://www.example.com/api',
{
headers: {
'X-New-Header': 'Foo',
},
},
);Replace headers: return NextResponse.rewrite(
'https://www.example.com/api',
{
request: {
headers: new Headers(
{
'Content-type': 'application/json',
'X-New-Header': 'Foo',
},
),
}
},
); |
Beta Was this translation helpful? Give feedback.
1 reply
-
|
That’s not what middleware is for. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
My understanding is that Next.js middleware can only intercept internal Routes. This means I cannot use the middleware to embed Auth tokens in the Header and continue the request to External URL (third-party domain).
Is there a technical solution on how it can be achieved?
Beta Was this translation helpful? Give feedback.
All reactions