Next.js Discord

Discord Forum

308 Permanent Redirect

Answered
French Lop posted this in #help-forum
Open in Discord
French LopOP
I use an external API which does not allow CORS. So, as far as I know, I have to set up a redirect or proxy. I thought I could do that via next.config.

trailingSlash: true, async rewrites() { return [ { source: '/api/v1/token/new/', destination: 'https://external-api.com/api/v1/token/new/', }, ] },

The problem I have now is that my requests are failing. I get a 308 Permanent Redirect error.
The request goes to the local host address
Answered by French Lop
Fixed it.

`const nextConfig = {
trailingSlash: true,
async rewrites() {
return [
{
source: '/api/v2/token/new/',
destination: 'https://external-api-example/api/v2/token/new/',
},
]
},
}``
View full answer

10 Replies

in your external api, do you have a redurect url set up? (like something that it goes to after compleation)
also for tokens, i think it may be better to do that with an actual api route to give more flexibility (ie this example, bot other just proxy things rewrites can be better)
French LopOP
there is an option for setting a redirect but for /token/new i just get application/json response with the token
used create-react-app before and there i just added a proxy to my package.json for the base adress like "www.external-api.com"
can you explain what "proxy to my package.json" means
French LopOP
"proxy": "https://api.com"
French LopOP
Fixed it.

`const nextConfig = {
trailingSlash: true,
async rewrites() {
return [
{
source: '/api/v2/token/new/',
destination: 'https://external-api-example/api/v2/token/new/',
},
]
},
}``
Answer
so other than using v2, was there any changes?
French LopOP
maybe i made typo mistake just did it again and restartet the app then it worked
nice that it works 🙂