javascript fetch 响应 403 错误

Posted

技术标签:

【中文标题】javascript fetch 响应 403 错误【英文标题】:javascript fetch responding with a 403 error 【发布时间】:2021-03-09 10:34:23 【问题描述】:

我正在尝试制作一个显示与用户厨房用品相关的食谱的食谱应用。 我正在使用带有rapidapi的spoonacular api,并且我不断收到403的获取结果,有人可以帮忙吗?

这是我的代码:

const fetch = require("node-fetch");
fetch("https://spoonacular-recipe-food-nutrition-v1.p.rapidapi.com/recipes/findByIngredients?ingredients=apples%2Cflour%2Csugar&number=5&ranking=1&ignorePantry=true", 
    "method": "GET",
    "headers": 
        "x-rapidapi-key": "I used the premade  rapidapi key from their website examples, i think that thats my key",
        "x-rapidapi-host": "spoonacular-recipe-food-nutrition-v1.p.rapidapi.com"
    
)
.then(response => 
    console.log(response);
)
.catch(err => 
    console.error(err);
);

这是我得到的结果:

Response 
  size: 0,
  timeout: 0,
  [Symbol(Body internals)]: 
    body: PassThrough 
      _readableState: [ReadableState],
      _events: [Object: null prototype],
      _eventsCount: 2,
      _maxListeners: undefined,
      _writableState: [WritableState],
      allowHalfOpen: true,
      [Symbol(kCapture)]: false,
      [Symbol(kTransformState)]: [Object]
    ,
    disturbed: false,
    error: null
  ,
  [Symbol(Response internals)]: 
    url: 'https://spoonacular-recipe-food-nutrition-v1.p.rapidapi.com/recipes/findByIngredients?ingredients=apples%2Cflour%2Csugar&number=5&ranking=1&ignorePantry=true',
    status: 403,
    statusText: 'Forbidden',
    headers: Headers  [Symbol(map)]: [Object: null prototype] ,
    counter: 0
  

如果我写 response.body 而不是有人评论的结果是:

PassThrough 
  _readableState: ReadableState 
    objectMode: false,
    highWaterMark: 16384,
    buffer: BufferList  head: [Object], tail: [Object], length: 1 ,
    length: 49,
    pipes: [],
    flowing: null,
    ended: true,
    endEmitted: false,
    reading: false,
    sync: false,
    needReadable: false,
    emittedReadable: false,
    readableListening: false,
    resumeScheduled: false,
    errorEmitted: false,
    emitClose: true,
    autoDestroy: true,
    destroyed: false,
    errored: null,
    closed: false,
    closeEmitted: false,
    defaultEncoding: 'utf8',
    awaitDrainWriters: null,
    multiAwaitDrain: false,
    readingMore: false,
    decoder: null,
    encoding: null,
    [Symbol(kPaused)]: null
  ,
  _events: [Object: null prototype] 
    prefinish: [Function: prefinish],
    error: [Function (anonymous)]
  ,
  _eventsCount: 2,
  _maxListeners: undefined,
  _writableState: WritableState 
    objectMode: false,
    highWaterMark: 16384,
    finalCalled: false,
    needDrain: false,
    ending: true,
    ended: true,
    finished: true,
    destroyed: false,
    decodeStrings: true,
    defaultEncoding: 'utf8',
    length: 0,
    writing: false,
    corked: 0,
    sync: false,
    bufferProcessing: false,
    onwrite: [Function: bound onwrite],
    writecb: null,
    writelen: 0,
    afterWriteTickInfo: null,
    buffered: [],
    bufferedIndex: 0,
    allBuffers: true,
    allNoop: true,
    pendingcb: 0,
    prefinished: true,
    errorEmitted: false,
    emitClose: true,
    autoDestroy: true,
    errored: null,
    closed: false
  ,
  allowHalfOpen: true,
  [Symbol(kCapture)]: false,
  [Symbol(kTransformState)]: 
    afterTransform: [Function: bound afterTransform],
    needTransform: false,
    transforming: false,
    writecb: null,
    writechunk: null,
    writeencoding: 'buffer'
  

【问题讨论】:

检查实际响应正文包含的内容,您可能会在其中找到人类可读的错误消息。 感谢您的回复,当我写它仍然不起作用时,我编辑了帖子,如果我这样做会发生什么:) 【参考方案1】:

我假设您已经订阅了这个 API。我检查了这个 API,下面的代码对我来说运行良好。

fetch("https://spoonacular-recipe-food-nutrition-v1.p.rapidapi.com/recipes/findByIngredients?ingredients=apples%2Cflour%2Csugar&number=5&ignorePantry=true&ranking=1", 
    "method": "GET",
    "headers": 
        "x-rapidapi-host": "spoonacular-recipe-food-nutrition-v1.p.rapidapi.com",
        "x-rapidapi-key": "**************************************"

)
.then(response => 
    console.log(response);
)
.catch(err => 
    console.error(err);
);

通常,403 状态码的主要原因是 URL 输入错误。但是,您的代码和我的代码之间的唯一区别在于 URL — 查询参数的重新排列。正确编写的应用程序不是敏感的,但有时如果应用程序编写得不好,这很重要。因此,您可以在 code-sn-p 生成器中使用 RapidAPI 提供的代码。

此外,403 状态码有时是由浏览器本身引起的。所以如果错误仍然存​​在,请在切换浏览器后尝试。

请记住,您可以随时写信给 RapidAPI 的支持团队 (support@rapidapi.com)。

【讨论】:

以上是关于javascript fetch 响应 403 错误的主要内容,如果未能解决你的问题,请参考以下文章

如何从 fetch javascript 请求的响应中获取数据

Javascript fetch api:无法检索某些响应标头

Javascript - 如何知道Fetch API中的响应类型?

Javascript fetch 无法从 GMail API Batch 请求中读取 Multipart 响应

Django 使用 Fetch 对 POST 请求返回 403 错误

npm publish报错403 Forbidden - PUT http://XXX - forbidden;http fetch PUT 403 http://XXX ; verbose stac