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

Outpout(console)(都市は「モントリオール」):
`λノードアプリ
送信: http: //api.openweathermap.org/data/2.5/weather?q = montreal&units = metric&appid = APIKEY

成功:CA`

ここに表示されているように、リクエストは成功です。 ブラウザから「Chamberí」などの特殊文字が含まれる都市を試してみると、すべてうまくいきます。URLは次のようになります。
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を使用してみましたが、どちらも同じエラーを返します。

これは何か問題があり、それがモジュールの私であるかどうかはわかりません。 私が助けを得ることができれば、それは非常にありがたいです:)

感謝。

(問題のリクエストに問題がある場合は申し訳ありませんが、これを行うのは初めてです:))

あなたの環境

| ソフトウェア| バージョン
| ---------------- | -------
| リクエスト| [email protected]
| ノード| v10.9.0
| npm | 6.2.0
| オペレーティングシステム| Windows 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 評価

関連する問題

gajus picture gajus  ·  29コメント

nijikokun picture nijikokun  ·  61コメント

warpdesign picture warpdesign  ·  29コメント

reconbot picture reconbot  ·  86コメント

pho3nixf1re picture pho3nixf1re  ·  41コメント