We are trying to fetch data from our Strapi CMS inside Cloudflare Workers. But we are facing an issue.
Cloudflare Workers is unable to fetch data from the CMS. response.json()
is throwing an error, Unexpected token < in JSON at position 0
. So, I thought it’s returning HTML instead of JSON.
And response.text()
returns the following HTML:
<!DOCTYPE html><html lang=\"en\">\n<head>\n<meta charset=\"utf-8\">\n<title>Page not found</title>\n</head>\n<body style=\"overflow: hidden; background-color: #2B4B50;\">\n <div style=\"display: flex; flex-wrap: wrap; justify-content: center; align-items: center; height: 100vh;\">\n \t<div role=\"main\">\n \t\t<div style=\"display:flex; justify-content:center;\">\n \t\t\t<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 252.1 173.4\" width=\"50%\"><path d=\"M202.4,61.9a78.1,78.1,0,0,0-152.8,0,55.9,55.9,0,0,0,6.3,111.5H196.2a55.9,55.9,0,0,0,6.2-111.5Zm-41.8,64.3a8.4,8.4,0,0,1,0,11.9,8.7,8.7,0,0,1-6,2.5,8.5,8.5,0,0,1-5.9-2.5L126,115.4l-22.6,22.7a8.7,8.7,0,0,1-6,2.5,8.5,8.5,0,0,1-5.9-2.5,8.4,8.4,0,0,1,0-11.9l22.7-22.7L91.5,80.9A8.4,8.4,0,0,1,103.4,69L126,91.7,148.7,69a8.4,8.4,0,0,1,11.9,11.9l-22.7,22.6Z\" transform=\"translate(0 0)\" style=\"fill:red\"></path></svg>\n \t\t</div>\n \t<h1 style=\"width: 100%; text-align: center; color: #fff;\">This page can not be found.</h1>\n \t</div>\n </div>\n</body></html>
IDK why the worker is not able to find the URL. The exact same code works everywhere I tried.
The code is private, so it’s not possible to share the code. But I can the share the structure of the code:
const query = `
{
# a graphql query
}
`
const response = await fetch('https://cms.example.com/graphql', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
query,
}),
})
const { data } = await response.json()
I have tried converting the GraphQL query to REST API calls. But it returns the exact same HTML response.
The CMS is also running on the standard port, 443. So, it’s not an issue. I have checked if adding :443
after the URL solves the issue. But it throws the same error.
Any help would be much appreciated.