0

If I have this POST request to an API:

fetch('/api/url/etc', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    username: 'exampleUsername',
    password: 'examplePassword',
  }),
})

How do I access the request's body? Here is where I'm starting with the API endpoint:

import type { RequestHandler } from '@builder.io/qwik-city'

export const onPost: RequestHandler = async () => {}

Not really sure where to go from here though.

Benjamin Buch
  • 4,752
  • 7
  • 28
  • 51
PiWizard3852
  • 95
  • 1
  • 11

1 Answers1

0

You can access the request body through the first argument of the function.

export const onPost: RequestHandler = async (requestEvent) => {
  const jsonBody = await requestEvent.request.json()

  const textBody = await requestEvent.request.text()

  const readableStream = requestEvent.request.body
}
Dale
  • 16
  • 1