I’m attempting to use a redirect to get around CORS issues when using ipify’s API (like this example in the docs).
I have my netlify.toml set up like this:
[[redirects]]
from = "/cors-proxy/*"
to = "https://geo.ipify.org/api/v1:splat"
status = 200
force = true
When I make a request to /cors-proxy/?apiKey=${apiKey} using Netlify Dev locally it works but when I deploy the app to Netlify I’m getting a 502 bad gateway.
Thanks for writing such a detailed thread here! I am not a headers expert, but here is a great thread on debugging a 502 that may be of service to you. It sounds like a similar scenario of working locally, but not once it is deployed to Netlify:
Let us know if following those steps work! If not, I can loop in our Support team as well.
Hi @hillary, Thank you for the suggestion. I am very much not an expert when it comes to this.
I followed that user’s example and checked if it was a matter of syntax support in different Node versions and that doesn’t seem to be the source of the problem. Both the Netlify build and my local machine are running Node 16.8.0.
I had a look at the headers that are being sent and can’t see if they’re making a difference.
I’ve tried the app in private windows in both Firefox (Developer Edition Version 92.0b9 64-bit) and Chrome (Official Build Version 92.0.4515.159 64-bit) to see if the browser or any extensions were interfering with things and it made no difference. It worked from localhost but not https://goofy-spence-1d3ac9.netlify.app.
The problem is that, API docs | IP Geolocation API is not responding to the request within 30 seconds. For proxy rewrites, we close the connection in 30 seconds and since the request is not complete by then, we return a 502.
Now, I don’t have an answer as to why the API is not answering requests made through proxy. Maybe they block such requests or something? Their support can answer that.
You can try to deploy this as a Netlify Function. Hopefully, the server will respond to requests when called from within Lambda and you can then pass that data to the client. It will also help you keep your API key safe.
Thank you very much @hrishikesh. Moving the fetch request into a serverless function did the trick and now I’m able to get a response from ipify to the deployed app.