Search code examples
bashapicurlweather-api

How to replicate command string to URL on cgywin/curl?


Currently I am using the following url string to obtain weather data from openweathermap.org.

Example: http://api.openweathermap.org/data/2.5/weather?q=New%20York&appid={apikey}

In response I receive

{"coord":{"lon":-74.01,"lat":40.71},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04d"}],"base":"cmc stations","main":{"temp":264.268,"pressure":1033,"humidity":53,"temp_min":264.268,"temp_max":264.268,"sea_level":1048.16,"grnd_level":1033},"wind":{"speed":1.62,"deg":73.5001},"clouds":{"all":68},"dt":1455544776,"sys":{"message":0.0048,"country":"US","sunrise":1455536990,"sunset":1455575462},"id":5128581,"name":"New York","cod":200}

I would like to replicate the above on cygwin/curl.

Below is one of my many failed attempts.

$ curl -X POST --data "q=London&appid={apikey}" http://api.openweathermap.org/data/2.5/weather
{"cod":401, "message": "Invalid API key. Please see http://openweathermap.org/faq#error401 for more info."}

But the API key works if I use directly on the URL

Question: How can I access this API using curl?

References:


Solution

  • It is just bash, yes? I don't have an API key, but this should work if I did.

    OPENWEATHER_API_KEY="something"
    curl "http://api.openweathermap.org/data/2.5/weather?q=New%20York&appid=$OPENWEAHTER_API_KEY"
    

    Output

    {"cod":401, "message": "Invalid API key. Please see http://openweathermap.org/faq#error401 for more info."}