Remix api 解析请求体
我正在创建一个将处理 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你可以这样做:
You can do: