Home > Net >  Sending Body with HTTP POST method in Qwik City
Sending Body with HTTP POST method in Qwik City

Time:02-03

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!

CodePudding user response:

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: {…},
})
  • Related