首页 > 解决方案 > 在 nodejs 中发布 url 编码的 Api 但仍然出现错误

问题描述

嗨,我是 javascript 新手,我尝试从 lala.ai 发布 url 编码的 Api。我按照互联网上的说明进行操作,但仍然出现错误。下面我把代码和结果。

//This is the instruction

POST /api/preview/
    Puts a file in the preview queue (the first minute of vocals).

Parameters (form-urlencoded):
    id (str): File id obtained from /upload/ method.
    filter_type (int, optional): Number of postprocess iterations with MWF.
    webpush-callback (json, optional): Client data for sending push notifications.

Returns (json):
    {
        "status": "success" | "error"
        "error": Error description if the status is "error"
    }

Examples:
    $ curl --url https://www.lalal.ai/api/preview/ --form-string "id=9a3ae258-7693-4046-87c2-ef577eb752bb" --form-string "filter_type=2"
    {"status": "success"}

    $ curl --url https://www.lalal.ai/api/preview/
    {"status": "error", "error": "No file id"}

这是我尝试过的

const qs = require("qs");

axios
  .post(
    "https://www.lalal.ai/api/preview/",
    {
      data: qs.stringify({
        id: "4d2f9262-e578-4290-97d3-43303fffbf56",
        filter_type: "2",
      }),
    },
    {
      headers: {
        "content-type": "application/x-www-form-urlencoded;charset=utf-8",
      },
    }
  )
  .then((result) => {
    console.log(result);
  });

这是我得到的错误结果

(node:34480) UnhandledPromiseRejectionWarning: Error: Request failed with status code 403
    at createError (D:\reactjs\upload_lalal\server\node_modules\axios\lib\core\createError.js:16:15)
    at settle (D:\reactjs\upload_lalal\server\node_modules\axios\lib\core\settle.js:17:12)
    at IncomingMessage.handleStreamEnd (D:\reactjs\upload_lalal\server\node_modules\axios\lib\adapters\http.js:244:11)
    at IncomingMessage.emit (events.js:327:22)
    at endReadableNT (_stream_readable.js:1327:12)
    at processTicksAndRejections (internal/process/task_queues.js:80:21)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:34480) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:34480) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

有人可以向我解释为什么这是错误以及如何解决吗?谢谢。

标签: javascriptnode.jsreactjsaxiosurlencode

解决方案


您的错误是... HTTP 403,这意味着

 The HTTP 403 is a HTTP status code meaning access to the requested resource is forbidden

您是否需要访问令牌才能使用 API?


推荐阅读