【问题标题】:What could cause an HTTP GET to return 504 in code but 404 via curl?什么可能导致 HTTP GET 在代码中返回 504 而通过 curl 返回 404?
【发布时间】:2017-05-30 13:06:39
【问题描述】:

代码如下:

  var request, tmUrl;

  request = require('request');

  tmUrl = "http://archive.is/timemap/https://www.washingtonpost.com/news/the-fix/wp/2017/01/15/rep-john-lewiss-books-sell-out-following-donald-trumps-attacks/";

  // this one works 
  request('http://www.google.com', function(error, response, body) {
    if (!error && response.statusCode === 200) {
      return console.log(body.slice(0, 301));
    }
  });

  // this one is always 504
  request(tmUrl, {
    timeout: 10000, // changing to 60s made no difference, 
                    // response comes back long before that 
    "User-Agent": 'curl/7.43.0'
  }, function(error, response, body) {
    if (!error) {
      if (response.statusCode === 200 || response.statusCode === 404) {
        return console.log(body.slice(0, 301));
      } else {
        console.error("response code ", response.statusCode);
        return console.log(body.slice(0, 301));
      }
    } else {
      return console.error(err);
    }
  });

问题是通过curl 运行该请求会返回预期的响应,即带有以下文本的404:TimeMap does not exists. The archive has no Mementos for the requested URI。但是当我在 Node 中运行它时,响应代码始终是 504,我不知道为什么。我最好的猜测是不允许使用用户代理,所以我让它们匹配。没用。我不知道下一步该做什么......

504 超时总是来自 cloudflare:

<span class="cf-footer-item"><span data-translate="performance_security_by">Performance
&amp; security by</span> <a data-orig-proto="https" data-orig-ref="www.cloudflare.com/5xx-error-landing?utm_source=error_footer" id="brand_link" target="_blank">CloudFlare</a>
</span>

这就是问题所在,cloudflare 是否会以某种我不知道的方式阻止对网站的编程访问?

【问题讨论】:

  • 启用 CURLOPT_VERBOSE ,并比较请求标头的差异。秘密就在这些标题中。
  • 啊,我在 curl 中尝试过,但在我的代码中没有。会做的,好建议。

标签: node.js curl httprequest


【解决方案1】:

504 请求无论如何都不会像网关超时那样返回结果。 为什么不在代码中处理它? 例如。 if (response.statusCode === 200 || response.statusCode === 404 || response.statusCode ===504) {

【讨论】:

  • 但是 504 与 404 的响应截然不同。我为什么要对它们一视同仁?
  • 取决于您想要实现的目标。 404 和 504 都是没有结果的错误代码。我做了一个小研究,发现 504 错误在云票价中很常见,您应该仔细检查服务器的配置和日志。
猜你喜欢
  • 1970-01-01
  • 2016-04-23
  • 1970-01-01
  • 2021-08-12
  • 1970-01-01
  • 1970-01-01
  • 2017-10-30
  • 2018-06-03
  • 1970-01-01
相关资源
最近更新 更多