Hello, it's been few days that am struggling with a problem. Am trying to fetch the information about a city with [openweathermap api](https://openweathermap.org/current. A normal request looks like this e.g:
http://api.openweathermap.org/data/2.5/weather?q=CITY&units=metric&appid=APIKEY
(you can reproduce this in your browser with a free api key)
result: (JSON)
{"coord":{"lon":-73.61,"lat":45.5},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}],"base":"stations","main":{"temp":24.65,"pressure":1012,"humidity":38,"temp_min":24,"temp_max":25},"visibility":48279,"wind":{"speed":7.2,"deg":280,"gust":11.3},"clouds":{"all":40},"dt":1535058000,"sys":{"type":1,"id":3829,"message":0.0049,"country":"CA","sunrise":1535018756,"sunset":1535068008},"id":6077243,"name":"Montreal","cod":200}
I use this function made with request module:
``
function req (url, jsonStatus){
const options = {
uri: url,
json: jsonStatus
}
console.log(`Sent: ${options.uri}`)
rp(options)
.then(repos => {
console.log(`Success: ${repos.sys.country}`) // Country code is returned
})
.catch(err => {
console.log(`Error: ${err}`)
})
}}
const owmUrl = `http://api.openweathermap.org/data/2.5/weather?q=${City}&units=metric&appid=${apiKey}`;
req(owmUrl, true);
Outpout(console)(where city is 'montreal'):
`λ node app
Sent: http://api.openweathermap.org/data/2.5/weather?q=montreal&units=metric&appid=APIKEY
Success: CA`
As you see here, the request is a success. When trying with cities with special characters as "Chamberí " from the browser, everything goes fine, the url looks like this:
http://api.openweathermap.org/data/2.5/weather?q=Chamberí&units=metric&appid=APIKEY
and result is (JSON again):
{"coord":{"lon":-3.71,"lat":40.44},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}],"base":"stations","main":{"temp":27.24,"pressure":1020,"humidity":32,"temp_min":27,"temp_max":28},"visibility":10000,"wind":{"speed":3.6,"deg":110},"clouds":{"all":0},"dt":1535059800,"sys":{"type":1,"id":5488,"message":0.0039,"country":"ES","sunrise":1535002448,"sunset":1535050778},"id":3117735,"name":"Chamberí","cod":200}
but if I send this with the code I made, I end up with this :
λ node app
Test
Sent: http://api.openweathermap.org/data/2.5/weather?q=Chamberí&units=metric&appid=0ec25df3f69726843a58a325b97e845d
Error: StatusCodeError: 404 - {"cod":"404","message":"city not found"}
I don't understand why I get this error since trying the same url in my browser works perfectly. I tried using the modules request-promise and request-promise-any but both of them return the same error.
The is something wrong this and I don't know if it's me of the module. If I could get help, it will be very appreciated :)
Thank.
(am sorry if something wrong with my issue request, it's the first time that I do this :) )
| software | version
| ---------------- | -------
| request | [email protected]
| node | v10.9.0
| npm | 6.2.0
| Operating System | windows 8.1 x64
| browser | opera
Encode the city name if it may contain non-ASCII characters:
console.log(encodeURIComponent('Montreal'));
console.log(encodeURIComponent('Montréal'));
console.log(encodeURIComponent('Chamberí'));
Output:
Montr%C3%A9al
Chamber%C3%AD
Data for Chamberí after encoding the name before making the request:
{ coord: { lon: -3.71, lat: 40.44 },
weather:
[ { id: 800, main: 'Clear', description: 'clear sky', icon: '01n' } ],
base: 'stations',
main:
{ temp: 21.98,
pressure: 1020,
humidity: 49,
temp_min: 21,
temp_max: 23 },
visibility: 10000,
wind: { speed: 1.5, deg: 40 },
clouds: { all: 0 },
dt: 1535068800,
sys:
{ type: 1,
id: 5488,
message: 0.0038,
country: 'ES',
sunrise: 1535088854,
sunset: 1535137170 },
id: 3117735,
name: 'Chamberí',
cod: 200 }
Thank you !!
:+1:
Thank you @davejagoda
Most helpful comment
How to encode URL parameters?
Encode the city name if it may contain non-ASCII characters:
Output:
Data for Chamberí after encoding the name before making the request: