简体   繁体   English

Remix api 解析请求正文

[英]Remix api parse request body

Im creating a api route that will handle a post request, the main idea is to create an api endpoint to add data.我正在创建一个将处理发布请求的 api 路由,主要思想是创建一个 api 端点来添加数据。 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但是当我使用邮递员使用 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

} }

Any idea how to parse the response and get the body data without having to install other packages?知道如何解析响应并获取正文数据而无需安装其他软件包吗?

You can do:你可以做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM