简体   繁体   English

在 Qwik City 中使用 HTTP POST 方法发送正文

[英]Sending Body with HTTP POST method in Qwik City

I have an API Route that uses the POST method:我有一个使用 POST 方法的 API 路由:

export const onPost = async () => {...}

I am sending a request to this API Route that includes a Body:我正在向这条包含正文的 API 路线发送请求:

                fetch('/api', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({...}),
                })

How do I access this body data on the API Route?我如何访问 API 路线上的身体数据? I have checed the Qwik/Qwik City documentation but the answer cannot be found in the docs.我查看了 Qwik/Qwik City 文档,但在文档中找不到答案。 There is too much data to be sent in a URL parameter and I have tried what I would do in another framework: URL 参数中要发送的数据太多,我尝试了在另一个框架中执行的操作:

export const onPost = async (req, res) => {
const body = req.body
...
}

But the server says that body is undefined.但是服务器说body是未定义的。 I know that Qwik City has a built in Request parameter for API Routes, but I cannot find any way to use this to get the request's body data.我知道 Qwik City 有一个用于 API 路由的内置Request参数,但我找不到任何方法来使用它来获取请求的正文数据。 TYA!蒂亚!

Using the useEndpoint API, I was able to add a body.使用useEndpoint API,我能够添加正文。 Just like with the fetch() API, you can do something like this:就像fetch() API 一样,你可以这样做:

const data = useEnpoint(‘/api/route’, {
method: ‘POST’,
body: {…},
headers: {…},
})

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

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