【问题标题】:Getting an empty response from HERE GEO API using fetch i Node.js使用 fetch i Node.js 从 HERE GEO API 获取空响应
【发布时间】:2020-11-03 14:20:15
【问题描述】:

我正在尝试从 node.JS 代码向“HERE”Geo API 发送请求并得到一个空响应
这就是我正在做的:

const fetch = require('node-fetch');
const http = require('http');
const https = require('https');
const keepAliveAgent = new https.Agent({
    keepAlive: true
});

var lat = '43.293162'
var long = '-85.920004'
var endpoint = 'https://revgeocode.search.hereapi.com/v1/revgeocode?at=' + lat + '%2C' + long + '&apiKey={API_key}'
console.log(endpoint)
here = async function (endpoint) {
    var start_time = new Date().getTime();

    let response = await fetch(endpoint, {
        method: 'GET',
        agent: keepAliveAgent

    });
    console.log(response)
    var time = { 'Here Response': + (new Date().getTime() - start_time) + 'ms' };
    console.log(time)
    return [response.json(), time];

}

回复是:

[
    {},
    {
        "Here Response": "781ms"
    }
]

当我在邮递员中使用带有 GET 请求的相同 URL 时,我得到了正确的响应 我做错了什么?

【问题讨论】:

  • 你可以试试把, 换成%2C 吗?
  • 您能告诉我们您使用的是哪个 Here Geo API 吗?一个网址会更好。
  • 我尝试使用 ',' 而不是 %2C 我仍然得到一个空响应
  • 我使用的网址是revgeocode.search.hereapi.com/v1/…{APIKEY}
  • @OferB 我在询问 API 名称。能否告诉我 API 名称或提供 API 文档的 URL?

标签: node.js geolocation node-fetch


【解决方案1】:

我使用了这段代码,它成功了
谢谢

const fetch = require('node-fetch');
const http = require('http');
const https = require('https');
const keepAliveAgent = new https.Agent({
    keepAlive: true
});

var lat = '43.293162'
var long = '-85.920004'
var endpoint = 'https://revgeocode.search.hereapi.com/v1/revgeocode?at=' + lat + '%2C' + long + '&apiKey={API_key}'
console.log(endpoint)
here = async function (endpoint) {
    var start_time = new Date().getTime();

       let response = await fetch(endpoint, {
      method: 'GET',
      redirect: 'follow',
      agent: keepAliveAgent ,
      
    })
  let data = await response.json()
  console.log(data) 
  var time = { 'Here Response': + (new Date().getTime() - start_time) + 'ms' };
  console.log(time)
  return [data];

}

【讨论】:

    猜你喜欢
    • 2020-02-01
    • 2017-01-04
    • 2017-07-09
    • 2017-09-07
    • 2022-07-26
    • 2020-08-02
    • 2019-10-03
    • 2017-09-06
    • 1970-01-01
    相关资源
    最近更新 更多