简体   繁体   中英

Sending Body with HTTP POST method in Qwik City

I have an API Route that uses the POST method:

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

I am sending a request to this API Route that includes a Body:

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

How do I access this body data on the API Route? I have checed the Qwik/Qwik City documentation but the answer cannot be found in the docs. There is too much data to be sent in a URL parameter and I have tried what I would do in another framework:

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

But the server says that body is undefined. 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. TYA!

Using the useEndpoint API, I was able to add a body. Just like with the fetch() API, you can do something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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