The worker fetches the following link: https://paxidocloud.com/user.php wich returns json data and a application/json header.
The problem is, that Cloudflare does not detect correctly that the feteched data is json, I am referring to your example Fetch JSON · Cloudflare Workers docs and just modified it with console.log to understand the detected data type. The opposite script also returns the header:
cf-apo-via: origin,cookie
cf-cache-status: BYPASS
cf-ray: 67c87fc1ec380c5f-SOF
content-encoding: br
content-type: application/json
date: Tue, 10 Aug 2021 10:21:44 GMT
expect-ct: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
nel: {"success_fraction":0,"report_to":"cf-nel","max_age":604800}
report-to: {"endpoints":[{"url":"https:\/\/a.nel.cloudflare.com\/report\/v3?s=BDp9cVf3fJeO4y8YWBdGXEcJrPzzToEV%2BcymjYfH6%2BOg7CUGLPdqerIeOXeafZJTZCZoPEh2IMlOUgemiUNCEnumfmbtyt7sH1jf4hs%2Be8gCaP52urKcz7KnajMhKy8kdg%3D%3D"}],"group":"cf-nel","max_age":604800}
server: cloudflare
vary: Accept-Encoding
with the data:
{"vorname":"Niko","nachname":"Test","email":"[email protected]","plz":"19010","anrede":"Mr"}
In my tests some times it correctly returns json, but mostly it returns just html.
Here is the worker code:
/**
* Example someHost is set up to take in a JSON request
* Replace url with the host you wish to send requests to
* @param {string} someHost the host to send the request to
* @param {string} url the URL to send the request to
*/
const someHost = "https://xyz.com"
const url = someHost + "/user.php"
/**
* gatherResponse awaits and returns a response body as a string.
* Use await gatherResponse(..) in an async function to get the response body
* @param {Response} response
*/
async function gatherResponse(response) {
const { headers } = response
const contentType = headers.get("content-type") || ""
console.log(contentType)
if (contentType.includes("application/json")) {
return JSON.stringify(await response.json())
}
else if (contentType.includes("application/text")) {
return response.text()
}
else if (contentType.includes("text/html")) {
return response.text()
}
else {
return response.text()
}
}
async function handleRequest() {
const init = {
headers: {
"content-type": "application/json;charset=UTF-8",
},
}
const response = await fetch(url, init)
const results = await gatherResponse(response)
return new Response(results, init)
}
addEventListener("fetch", event => {
return event.respondWith(handleRequest())
})
Thank you