I’m having trouble with a proxy redirect in my netlify.toml file.
[[redirects]]
from = "/api/current-conditions/"
to = "https://api.openweathermap.org/data/2.5/weather?units=imperial&APPID=OPENWEATHER_API_KEY_REPLACE"
query = {q = ":q"}
status = 200
force = true
headers = {X-From = "Netlify"}
The API Key is being injected to the redirect rule on build (as described here: File-based configuration | Netlify Docs), which is working perfectly on my local machine using Netlify Dev (console shows “Proxying to…” on API calls, data is returned without errors). Despite no build errors, when I deploy my branch to Netlify the API I am calling returns an error.
I’m not sure why this would work locally with Netlify Dev but not on Netlify.app after deploying- any help here is greatly appreciated!
For troubleshooting, I’m also curious if its possible to see redirect logs on a deployment, similar to what Netlify Dev shows in the console on my local machine any time it executes a redirect.
is that we proxy to your api server using the correct API Key (which I won’t share in this post), and your server returns an HTTP 400 status with the message you mention. However, when I try to use the URL directly (the one in that redirect, in your netlify.toml file associated with that deploy), I get an HTTP 401 unauthorized instead. You can see the two queries to your server - one from Netlify, and one from me directly, at:
(browser, via netlify) 20:18 UTC today
(me, direct, HTTP 401) 20:22 UTC today
Do I need some POST data or is it intended to be a GET call, for my testing? What calls do you see on your server at those times?
The redirect rule should be passing on the q=Denver query but seems to not be in production. The rule works fine when testing locally with Netlify Dev (OPENWEATHER_API_KEY_REPLACE is replaced with the valid API key during build)
[[redirects]]
from = "/api/current-conditions/"
to = "https://api.openweathermap.org/data/2.5/weather?units=imperial&APPID=OPENWEATHER_API_KEY_REPLACE"
query = {q = ":q"}
status = 200
force = true # COMMENT: ensure that we always redirect
headers = {X-From = "Netlify"}
howdy, just letting you know fool is out on PTO this week, and thats why we haven’t responded to you yet. We will try and get back to you as soon as we can, though! thanks for your patience.
Hey @onecheesepizza!
I think the query in the netlify.toml applies to the “from” part- so when you define the query as {"q = :q"}, you’re saying that after the path "/api/current-conditions/" there will be a query param.
You can then use that param in the “to” path.
So I think you need something like: https://api.openweathermap.org/data/2.5/weather?units=imperial&q=:q&APPID=OPENWEATHER_API_KEY_REPLACE
Wanna give that a shot and let us know if it works better?
[[redirects]]
from = "/api/current-conditions/"
to = "https://api.openweathermap.org/data/2.5/weather?q=:q&units=imperial&APPID=OPENWEATHER_API_KEY_REPLACE"
query = {q = ":q"}
status = 200
force = true
headers = {X-From = "Netlify"}
For what its worth, when using Netlify Dev on my local machine, the redirect works perfect with the query only represented as query = {q = ":q"} in the redirect rule without the q=q: also being reflected in ‘to’ URL of the redirect rule. It is only when pushed to production that the redirect failed.
In the Netlify documentation (Redirects and rewrites | Netlify Docs), the example redirect also does not reflect the need to put the query itself in the redirect url:
[[redirects]]
from = "/old-path"
to = "/new-path"
status = 301
force = false
query = {path = ":path"}
conditions = {Language = ["en"], Country = ["US"], Role = ["admin"]}[[redirects]]
from = "/old-path"
to = "/new-path"
status = 301
force = false
query = {path = ":path"}
conditions = {Language = ["en"], Country = ["US"], Role = ["admin"]}
Should this example’s ‘to’ path should actually be /new-path?path=:path ?