Remix api 解析请求体

发布于 2025-01-12 19:45:54 字数 2001 浏览 2 评论 0原文

我正在创建一个将处理 post 请求的 api 路由,主要思想是创建一个 api 端点来添加数据。问题是我无法将数据发送到端点。

// posts-creation.ts
export const action: ActionFunction = async ({ request }) => {
  switch (request.method) {
      case 'POST': {
        return json(request.body);
      }
  }
}

但是当我使用邮递员使用 JSON 正文进行发布请求时,会显示此

{
"_readableState": {
    "objectMode": false,
    "highWaterMark": 16384,
    "buffer": {
        "head": null,
        "tail": null,
        "length": 0
    },
    "length": 0,
    "pipes": [],
    "flowing": null,
    "ended": false,
    "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,
    "dataEmitted": false,
    "decoder": null,
    "encoding": null
},
"_events": {
    "error": [
        null,
        null,
        null,
        null
    ]
},
"_eventsCount": 5,
"_writableState": {
    "objectMode": false,
    "highWaterMark": 16384,
    "finalCalled": false,
    "needDrain": false,
    "ending": false,
    "ended": false,
    "finished": false,
    "destroyed": false,
    "decodeStrings": true,
    "defaultEncoding": "utf8",
    "length": 0,
    "writing": false,
    "corked": 0,
    "sync": true,
    "bufferProcessing": false,
    "writecb": null,
    "writelen": 0,
    "afterWriteTickInfo": null,
    "buffered": [],
    "bufferedIndex": 0,
    "allBuffers": true,
    "allNoop": true,
    "pendingcb": 0,
    "prefinished": false,
    "errorEmitted": false,
    "emitClose": true,
    "autoDestroy": true,
    "errored": null,
    "closed": false
},
"allowHalfOpen": true

信息}

知道如何解析响应并获取正文数据而无需安装其他包吗?

Im creating a api route that will handle a post request, the main idea is to create an api endpoint to add data. The problem is I can not get the data sent to the endpoints.

// posts-creation.ts
export const action: ActionFunction = async ({ request }) => {
  switch (request.method) {
      case 'POST': {
        return json(request.body);
      }
  }
}

But when I use postman to do post request with a JSON body shows this

{
"_readableState": {
    "objectMode": false,
    "highWaterMark": 16384,
    "buffer": {
        "head": null,
        "tail": null,
        "length": 0
    },
    "length": 0,
    "pipes": [],
    "flowing": null,
    "ended": false,
    "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,
    "dataEmitted": false,
    "decoder": null,
    "encoding": null
},
"_events": {
    "error": [
        null,
        null,
        null,
        null
    ]
},
"_eventsCount": 5,
"_writableState": {
    "objectMode": false,
    "highWaterMark": 16384,
    "finalCalled": false,
    "needDrain": false,
    "ending": false,
    "ended": false,
    "finished": false,
    "destroyed": false,
    "decodeStrings": true,
    "defaultEncoding": "utf8",
    "length": 0,
    "writing": false,
    "corked": 0,
    "sync": true,
    "bufferProcessing": false,
    "writecb": null,
    "writelen": 0,
    "afterWriteTickInfo": null,
    "buffered": [],
    "bufferedIndex": 0,
    "allBuffers": true,
    "allNoop": true,
    "pendingcb": 0,
    "prefinished": false,
    "errorEmitted": false,
    "emitClose": true,
    "autoDestroy": true,
    "errored": null,
    "closed": false
},
"allowHalfOpen": true

}

Any idea how to parse the response and get the body data without having to install other packages?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抽个烟儿 2025-01-19 19:45:54

你可以这样做:

const data = await request.json();
return json({ data });

You can do:

const data = await request.json();
return json({ data });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文