i have tried getting the post params for a worker but cannot.
addEventListener(‘fetch’, event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
const { searchParams } = new URL(request.url);
const url = “https://www.testserver.com/test.php?” + “auth=”+JSON.stringify(request);
let thing = {
method: ‘GET’
}
fetch(url,thing);
return new Response();
}
even dumping the whole request stringified , i couldnt find any of my input post params… what am i doing wrong???
With a post, the contents show up as the body as a readable stream, so you do something like:
const requestObject = await request.json();
const requestString = JSON.stringify(requestObject);
Make sure to put the await in there since that json() function is asynchronous.
im new to cloudflare and appreciate your response…
addEventListener(‘fetch’, event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
var requestObject = await request.json();
var requestString = JSON.stringify(requestObject);
return new Response(requestString, {status: 200});
}
causes a 500 internal error. this is in the playground using post with test fields in the body
found a working solution:
addEventListener(‘fetch’, event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
let body = await request.text()
let formData = new URLSearchParams(body)
//var test = str.replace(/\s+/g, ‘’);
//var test = JSON.stringify(body).replace(/\s+/g, ‘’)
//var test = body;
const params = {}
const queryString = body.split(‘&’)
queryString.forEach(item => {
const kv = item.split(‘=’)
if (kv[0]) params[kv[0]] = kv[1] || true
})
var test = JSON.stringify(params);
return new Response(test, { status: 200 });
}
thanks to @adancha