如何在cgywin / curl上将命令字符串复制到URL?

时间:2016-02-15 16:26:57

标签: bash api curl weather-api

目前我正在使用以下网址字符串从openweathermap.org获取天气数据。

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

作为回应,我收到了

{"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}

我想在cygwin / curl上复制上面的内容。

以下是我失败的尝试之一。

$ 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."}

但是如果我直接在URL

上使用API​​密钥

问题:如何使用curl访问此API?

参考文献:

1 个答案:

答案 0 :(得分:0)

这只是bash,是吗?我没有API密钥,但如果我这样做,这应该有用。

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

输出

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