1

I have the following URL:

https://forecast.weather.gov/zipcity.php?inputstring=95014

I would like to figure out which URL it will redirect to. In this example it is:

https://forecast.weather.gov/MapClick.php?CityName=Cupertino&state=CA&site=MTR&lat=37.3042&lon=-122.095

I tried multiple solutions such as:

res.headers.location

and

res.headers.get('location')

But none of them seem to work. I know that the URL redirects because, I could successfully redirect in curl and google-chrome. Here is the code that I am running:

https.get('https://forecast.weather.gov/zipcity.php?inputstring=95014', res => console.log(res.headers.location))

When I was running in curl, I ran the following:

curl -Ls -o /dev/null -w %{url_effective} http://forecast.weather.gov/zipcity.php?inputstring=95014

And got the desired output of:

http://forecast.weather.gov/MapClick.php?CityName=Cupertino&state=CA&site=MTR&lat=37.3042&lon=-122.095

When I run curl:

$ curl -I https://forecast.weather.gov/zipcity.php?inputstring=95014
HTTP/2 302
server: Apache
x-nids-serverid: www1.mo
location: https://forecast.weather.gov/MapClick.php?CityName=Cupertino&state=CA&site=MTR&lat=37.3042&lon=-122.095
x-ua-compatible: IE=Edge
access-control-allow-origin: *
content-type: text/html; charset=UTF-8
content-length: 0
cache-control: max-age=722
expires: Mon, 26 Apr 2021 19:28:03 GMT
date: Mon, 26 Apr 2021 19:16:01 GMT
strict-transport-security: max-age=31536000 ; includeSubDomains ; preload

As you can see, there are multiple headers present here that are not present inside nodejs's results:

{
  server: 'AkamaiGHost',
  'mime-version': '1.0',
  'content-type': 'text/html',
  'content-length': '288',
  expires: 'Mon, 26 Apr 2021 19:21:50 GMT',
  date: 'Mon, 26 Apr 2021 19:21:50 GMT',
  connection: 'close',
  'strict-transport-security': 'max-age=31536000 ; includeSubDomains ; preload'
}

I would like to stick to the http module.

Saiansh Singh
  • 583
  • 5
  • 16
  • Does this answer your question? [How do I get the redirected url from the nodejs request module?](https://stackoverflow.com/questions/16687618/how-do-i-get-the-redirected-url-from-the-nodejs-request-module) – Petr Hejda Apr 26 '21 at 18:54
  • I want to stick with the `http` module built into nodejs. – Saiansh Singh Apr 26 '21 at 19:00
  • Does this answer your question? [How do you follow an HTTP Redirect in Node.js?](https://stackoverflow.com/questions/7323932/how-do-you-follow-an-http-redirect-in-node-js) – esqew Apr 26 '21 at 19:02
  • 1
    All those answers either use another module or look at the header. For some reason the header is not present in my response even though the URL redirects. – Saiansh Singh Apr 26 '21 at 19:05
  • `https.get` doesn't seem to follow redirects at least by default, so unless it's a 301/302, the response URL should be the requested one. – Ismail Mar 16 '22 at 04:59

0 Answers0