Axios 和 fetch 响应来自同一外部端点的不同状态代码

Owl*_*Owl 5 javascript node.js fetch-api axios node-fetch

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

在邮递员和 chrome 上运行良好,但是当我尝试axios从 Node.js使用时,它响应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
Run Code Online (Sandbox Code Playgroud)

小智 1

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