一个国家/地区的5个城市的HTTP POST请求列表

时间:2017-11-21 04:10:14

标签: javascript node.js http http-post

我正在尝试使用HTTP Post Request从一个国家/地区获取5个城市的列表(例如:美国):

http://www.webservicex.net/globalweather.asmx?op=GetCitiesByCountry

这是我使用的库:https://www.npmjs.com/package/http-post

这是我迄今为止所做的,但它似乎不起作用:

var http = require('http');
http.post = require('http-post');

var url = 'http://www.webservicex.net/globalweather.asmx/GetCitiesByCountry';
var CountryName = 'United States';

http.post (url, CountryName, function(res) {

    res.setEncoding('utf8');
    res.on('data', function(chunk) {
        console.log(chunk);
    });

});

1 个答案:

答案 0 :(得分:0)

您没有将密钥名称发送到API。 只需修改代码即可执行以下操作:

var http = require('http');
http.post = require('http-post');

var url =
 'http://www.webservicex.net/globalweather.asmx/GetCitiesByCountry';
var CountryName = 'United States';

http.post (url, {CountryName:CountryName}, function(res) {

res.setEncoding('utf8');
res.on('data', function(chunk) {
    console.log(chunk);
});


});

请注意,我在“CountryName”变量中发送国家/地区名称。

相关问题