I cann't parse request body

For Workes & Pages, what is the name of the domain?

example.com

What is the issue or error you’re encountering

Functions text(), json(), formData(), and arrayBuffer() return null or empty string

What are the steps to reproduce the issue?

I try to process all incoming requests:

addEventListener(“fetch”, event => event.respondWith(handle(event)))

async function parseBodyAsJson(request) {
try {
return JSON.stringify(await request.json());
} catch (e) {
console.log('ERROR parseBodyAsJson ', {e});
return null
}
}

async function sendToAnotherService(body) {
const url = ‘https://myservice.com
const logRequest = new Request(url, {
method: “POST”,
body: JSON.stringify(body),
headers: {
“Content-Type”: “application/x-www-form-urlencoded”,
},
})

return logRequest
}

async function handle(event) {
const request = event.request
const response = await fetch(request)
const body = {
bodyJson: await parseBodyAsJson(event.request) // !!! HERE WE GOT JUST null !!!
}
const logRequest = await sendToAnotherService(body)
event.waitUntil(fetch(logRequest))

return response
}

But I always received bodyJson as null. Please help me

Well what error are you logging?

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.