【问题标题】:Error 403 when making a POST request with Axios on a specific API URL在特定 API URL 上使用 Axios 发出 POST 请求时出现错误 403
【发布时间】:2021-11-24 03:57:34
【问题描述】:

我在 Nodejs 环境中使用 Axios 来发出获取数据的请求,但是当我使用内容类型为“application/x-www-form-unlencoded”的 POST 请求时,我收到错误 403 的响应。这个 HTTP 请求的问题在哪里?,因为它适用于我的其他工具。这是我正在做的一个例子:

const axios = require('axios');

(async () => {
  try {

    let res = await axios({
      method: 'post',
      baseURL: 'https://www3.animeflv.net',
      url: '/api/animes/search',
      data: 'value=tokyo',
      headers: { 'User-Agent':'Mozilla/5.0 (X11; Linux x86_64)', 
                 'Content-Type': 'application/x-www-form-urlencoded'}
    })
    
    console.log(`statusCode: ${res.status}`);
    console.log(res.data.data);
  } catch (error) {
    console.error(error.response);
  }
})();

当我执行这个时,API 的响应是这样的:

另外当我在Linux中使用curl命令发出POST请求时,响应成功,如下图:

【问题讨论】:

  • 我认为您需要使用 headers: { 'content-type': 'application/x-www-form-urlencoded' } 将 content-type 标头添加到您的 axios 请求中

标签: javascript node.js forms http axios


【解决方案1】:

我认为您的代码运行良好,因为如果您查看收到的响应 (console.error(error.response.data);) 的“数据”部分,您会看到收到“检查您的浏览器”HTML 网页,即浏览器应该已经渲染了。

如果您使用像 Insomnia (Screenshot of the request in Insomnia) 这样的 API 客户端,您会得到相同的响应。

也许您必须使用另一个 api,或者生成一个执行 curl 以发出请求的 child_process,就像您在命令行中所做的那样。

【讨论】:

    猜你喜欢
    • 2021-06-07
    • 2019-08-22
    • 2020-10-26
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 2021-12-13
    • 1970-01-01
    相关资源
    最近更新 更多