0

I'm trying to get list of 5 cities in a country (Ex: United States) by using HTTP Post Request to get the cities from this website:

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

This is the library that I use: https://www.npmjs.com/package/http-post

This is what I've got so far but it doesn't seem to work:

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);
    });

});
Misuti
  • 313
  • 1
  • 2
  • 8

1 Answers1

0

You are not sending the key name to the API. Just modify your code to do something like this :

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);
});


});

Note that I am sending country name in 'CountryName' variable.

Ankit Bahuguna
  • 568
  • 3
  • 13
  • Thanks a lot. May I ask why is that the case? Sorry if it's a stupid question. Then how would i go about getting the # of cities that I want in that country? (for example like first 5 cities of US) – Misuti Nov 21 '17 at 05:07
  • In http post you need to send name, value pairs, you were just sending the value. https://stackoverflow.com/questions/14551194/how-are-parameters-sent-in-an-http-post-request To get list of cities you will have to parse the resulting xml response and extract the city names. – Ankit Bahuguna Nov 21 '17 at 05:13
  • So from my understanding is that, I've already got the 'data' in this 'chunk'. Now if i need to get list of cities then I have to parse this xml response 'chunk' then i can extract the city names. Is that correct? Sorry if my grammar confuses you. – Misuti Nov 21 '17 at 05:18
  • Yes. You will need some sort of xml parser library for that. – Ankit Bahuguna Nov 21 '17 at 05:22
  • Thanks a lot. I'd really appreciate that you take your time to help me out. – Misuti Nov 21 '17 at 05:26