[英]Nodejs equivalent of sending one GET request to the server to fetch data from the API
這是我嘗試從https://geocode.xyz/?locate=Warsaw,Poland&json=1
獲取數據的API端點。
當我在瀏覽器或郵遞員中運行它時,我得到了成功的響應。 這是預期的輸出片段。
{“ standard”:{“ addresst”:{},“ city”:“ Warsaw”,
“ prov”:“ PL”,“國家名”:“波蘭”,“郵政”:{},
“ confidence”:“ 0.7”},“ longt”:“ 21.03539”,“ alt”:{
“ loc”:{“ longt”:“ 21.23400”,“ prov”:“ PL”,
“ city”:“華沙”,“國家名稱”:“波蘭”,“郵政”:“ 05-077”,“區域”:{},“ latt”:“ 52.21890”}},“海拔”:{}, “ latt”:“ 52.23275”}
我正在嘗試使用nodejs獲得相同的輸出。 我嘗試了默認的https和request模塊,但無濟於事。 這就是我所擁有的:
const request = require('request');
request('https://geocode.xyz/?locate=Warsaw,Poland&json=1', { json: true }, (err, res, body) => {
if (err) { return console.log(err); }
console.log(body);
});
這是我看到的輸出:
{成功:錯誤,錯誤:{代碼:'006',消息:'請求已限制。' }}
免費版本的API每秒僅請求1個請求。
受限制的API訪問是免費的。 速率限制:每秒最多1個API調用。 API(對所有空閑端口用戶的總和,每秒節流到不超過1個請求。例如,如果同時有2個免費請求,則每個請求節流到2秒)。
如何限制我的代碼僅發送一個請求? 使用瀏覽器/郵遞員時如何獲得回復。 如何使用nodejs獲得相同的預期成功響應?
這也是我嘗試使用https進行的嘗試,並且得到了相同的響應:
const https = require('https');
https.get('https://geocode.xyz/?locate=Warsaw,Poland&json=1', (resp) => {
let data = '';
// A chunk of data has been recieved.
resp.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received. Print out the result.
resp.on('end', () => {
console.log(JSON.parse(data));
});
}).on("error", (err) => {
console.log("Error: " + err.message);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.