【问题标题】:Axios and fetch responded with different status code from same external endpointAxios 和 fetch 响应来自同一外部端点的不同状态代码
【发布时间】:2021-01-20 07:13:09
【问题描述】:

我正在尝试向此端点发送请求:https://9gag.com/v1/group-posts/group/default/type/trending 以获取 9gag 帖子数据。

在邮递员和 chrome 上运行良好,但是当我尝试使用 Node.js 中的 axios 时,它以 403 响应(并读取返回的 DOM,它认为它要求我输入验证码),当我尝试使用node-fetch 发送请求,它以200 回复并带有帖子数据(这是正确的)。

问题是,为什么node-fetch 可以正常工作,而axios 却不行?

这是我用于测试的代码:

import axios from "axios";
import fetch from "node-fetch";

const URL = "https://9gag.com/v1/group-posts/group/default/type/trending";

// Inside async function
await fetch(URL); // Responded with 200 and json data
await axios(URL); // Responded with 403 and HTML DOM, axios.get() also gives the same result

【问题讨论】:

  • 也许他们发送不同的user-agent 标头,这是决定提供验证码的一个因素?
  • 我尝试发送具有相同 user-agent 值的标头,但 axios 仍然无法正常工作。不知何故,axios 在repl.it I made 中也能正常工作
  • 查看实际发送的 http 请求以找出差异。

标签: javascript node.js axios fetch-api node-fetch


【解决方案1】:

如果您查看https://9gag.com/robots.txt,您可能会注意到Mediapartners-Google。这是一个用户代理,您必须在标头中指定才能绕过 Cloudflare 验证码。

【讨论】:

    猜你喜欢
    • 2022-08-21
    • 1970-01-01
    • 1970-01-01
    • 2021-10-06
    • 2021-04-12
    • 2017-03-11
    • 1970-01-01
    • 1970-01-01
    • 2017-08-13
    相关资源
    最近更新 更多