<p>请求不支持字符为:“í”、“ë”、“å”..</p>

创建于 2018-08-24  ·  3评论  ·  资料来源: request/request

概括

您好,这几天一直在为一个问题而苦苦挣扎。 我正在尝试使用 [openweathermap api](https://openweathermap.org/current. 获取有关城市的信息。正常请求如下所示:
http://api.openweathermap.org/data/2.5/weather?q= CITY &units=metric&appid= APIKEY

(您可以使用免费的 api 密钥在浏览器中重现此内容)

结果:(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}

我使用请求模块制作的这个函数:

``

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

输出(控制台)(其中城市是“蒙特利尔”):
`λ 节点应用程序
发送: http: //api.openweathermap.org/data/2.5/weather ? q=montreal&units=metric&appid= APIKEY

成功:CA`

如您所见,请求成功。 在浏览器中尝试使用特殊字符作为“Chamberí”的城市时,一切正常,网址如下所示:
http://api.openweathermap.org/data/2.5/weather?q=Chamberí&units=metric&appid= APIKEY

结果是(再次是JSON):
{"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}

但是如果我用我制作的代码发送这个,我最终会得到这个:

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

我不明白为什么会出现此错误,因为在浏览器中尝试相同的 url 效果很好。 我尝试使用模块 request-promise 和 request-promise-any 但它们都返回相同的错误。

这是错误的,我不知道是不是我的模块。 如果我能得到帮助,我将不胜感激:)

感谢。

(很抱歉,如果我的问题请求有问题,这是我第一次这样做:))

您的环境

| 软件| 版本
| ---------------- | -------
| 请求 | 请求@2.87.0
| 节点 | v10.9.0
| 每日头条 | 6.2.0
| 操作系统 | 视窗 8.1 x64
| 浏览器 | 歌剧

最有用的评论

如何编码 URL 参数?

如果城市名称可能包含非 ASCII 字符,请对其进行编码:

console.log(encodeURIComponent('Montreal'));
console.log(encodeURIComponent('Montréal'));
console.log(encodeURIComponent('Chamberí'));

输出:

Montr%C3%A9al
Chamber%C3%AD

在发出请求之前对名称进行编码后的 Chamberí 数据:

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

所有3条评论

如何编码 URL 参数?

如果城市名称可能包含非 ASCII 字符,请对其进行编码:

console.log(encodeURIComponent('Montreal'));
console.log(encodeURIComponent('Montréal'));
console.log(encodeURIComponent('Chamberí'));

输出:

Montr%C3%A9al
Chamber%C3%AD

在发出请求之前对名称进行编码后的 Chamberí 数据:

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

谢谢 !!

:+1:

谢谢@davejagoda

此页面是否有帮助?
0 / 5 - 0 等级