Proxy redirect in netlify.toml: query params work locally, but not after deploying

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.

{
"cod": "400",
"message": "Nothing to geocode"
}

Confirmed working destination API call (key removed for this post): https://api.openweathermap.org/data/2.5/weather?units=imperial&q=Denver&APPID=REMOVED_API_KEY_FOR_FORUM_POST

The non-working API call using the Netlify redirect, on a branch deploy: https://5f2e2b3b91db3b000752d71d--ocp-weather-dash.netlify.app/api/current-conditions?q=Reno

I’m able to reproduce the “Nothing to geocode” error on the destination API call if I drop the “&q=Denver” query (https://api.openweathermap.org/data/2.5/weather?units=imperial&APPID=REMOVED_API_KEY_FOR_FORUM_POST), so I’m thinking that its something with query = {q = ":q"} in my redirect rule.

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.

howdy @onecheesepizza and thanks for making me hungry at 1:20 pm local by username alone, well played! :slight_smile:

As far as I can tell, we are proxying correctly - what I see when I visit that URL:

https://5f2e2b3b91db3b000752d71d--ocp-weather-dash.netlify.app/api/current-conditions?q=Reno

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?

Thanks for looking at this @fool :pizza: .

Raw API Call
Assuming REMOVED_API_KEY_FOR_FORUM_POST is replaced with a valid API key…

When I call the raw API with GET
https://api.openweathermap.org/data/2.5/weather?units=imperial&q=Denver&APPID=REMOVED_API_KEY_FOR_FORUM_POST

I get the following response:

Invalid Raw API Call
The same GET without the q=Denver query predictably results in this 400 response:

This 400 response from a missing query matches the response I’m currently getting when using the Netlify redirect I defined in netlify.toml.

Redirected API Call
https://5f2e2b3b91db3b000752d71d--ocp-weather-dash.netlify.app/api/current-conditions?q=Denver
even though q=Denver is passed,
gets this response:
Screen Shot 2020-08-18 at 6.30.29 PM

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 :man_shrugging: (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.

1 Like

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?

1 Like

@jen Thanks for the assist! That is exactly what was missing. The previously broken API call redirect is now working (see https://5f7281fc09ff35000751ca0e–ocp-weather-dash.netlify.app/api/current-conditions?q=Denver).

My working redirect rule now looks like…

[[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 ?

Thanks again for your help.

@jen I just read the new post regarding changes to how redirects work here: Changes to Redirects with query string parameters are coming

Once deployed, will those changes mean that queries can be omitted from redirect rules completely moving forward?

indeed it will mean that :smiley:

1 Like

@fool haha right on. cheers to things becoming easier :beers:

1 Like