node.js - Node 中请求 GET 失败

标签 node.js httprequest

我正在从我的 node 后端向第 3 方 API 服务请求 GET
我收到403禁止的响应:

request("http://www.giantbomb.com/api/search/?api_key=my_api_key&field_list=name,image,id&format=json&limit=1&query=street%20fighter%203&resources=game",(err,res,body) => {
    console.log(body);
})

在浏览器中查询相同的请求会返回预期的结果。
知道为什么会发生这种情况吗?

编辑:

记录响应正文,我收到以下页面(没有 JS):

<h1>Wordpress RSS Reader, Anonymous Bot or Scraper Blocked</h1>
<p>
    Sorry we do not allow WordPress plugins to scrape our site. They tend to be used maliciously to steal our content. We do not allow scraping of any kind.
    You can load our RSS feeds using any other reader but you may not download our content.
    <a href='/feeds'>Click here more information on our feeds</a>
</p>
<p>
    Or you're running a bot that does not provide a unique user agent.
    Please provide a UNIQUE user agent that describes you. Do not use a default user agent like "PHP", "Java", "Ruby", "wget", "curl" etc.
    You MUST provide a UNIQUE user agent. ...and for God's sake don't impersonate another bot like Google Bot that will for sure
    get you permanently banned.
</p>
<p>
    Or.... maybe you're running an LG Podcast player written by a 10 year old. Either way, Please stop doing that.
</p>

最佳答案

此服务需要 header 中的 User-Agent,请参阅此示例

const rp = require('request-promise')

const options = {
  method: 'GET',
  uri: 'http://www.giantbomb.com/api/search/?api_key=my_api_key&field_list=name,image,id&format=json&limit=1&query=street%20fighter%203&resources=game',
  headers: { 'User-Agent': 'test' },
  json: true
}

rp(options)
  .then(result => {
    // process result
  })
  .catch(e => {
    // handle error
  })

关于node.js - Node 中请求 GET 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37607498/

相关文章:

javascript - 我应该如何从 Java 应用程序运行 NodeJS?

javascript - 无法将 ejs 值获取到脚本变量中?

javascript - 在 Node.js 中复制到剪贴板?

javascript - 环境变量在 Electron 中未定义,即使它已在 webpack.DefinePlugin 中设置

php - PECL_HTTP 无法识别 php ubuntu

javascript - 使用 Node.js 渲染页面时出现 css/js 文件引用错误

javascript - 使用 fs 读取文件后解析 Json

php - 将带有 formData 的数据发送到 mysql

java - 如何从 Java 中的 HTTP 请求中获取 JSON 对象

jsf - 如何从 View 范围的 JSF bean 中获取请求参数?