Fetch inside a worker fails if the worker is invoked via a Pages function binding

hi folks 👋 I have a problem with a worker that's invoked from a Pages function via bindings, and I'd appreciate some help. The worker is pretty simple, it uses fetch to download a JSON file, and then it responds with some data. When in call the worker directly using curl, it works as expected. However, when I call it using a Service binging from a Pages function, I get an error because the JSON file is invalid:
message:

"Unexpected token 'e', "error code: 522" is not valid JSON",
exception:
{
stack:

" at async Object.fetch (index.js:46139:27)",
name:

"SyntaxError",
message:

"Unexpected token 'e', "error code: 522" is not valid JSON",
timestamp:

1745518911797,
},
message:

"Unexpected token 'e', "error code: 522" is not valid JSON",
exception:
{
stack:

" at async Object.fetch (index.js:46139:27)",
name:

"SyntaxError",
message:

"Unexpected token 'e', "error code: 522" is not valid JSON",
timestamp:

1745518911797,
},
The code in the Pages function is trivial:
export async function onRequestPost(context) {
return context.env.WORKER.fetch(context.request);
}
export async function onRequestPost(context) {
return context.env.WORKER.fetch(context.request);
}
Does anyone know what I can do to make it work as expected? It looks like fetch has some kind of issue when it's used in Pages functions, but I cannot find anything in the docs.
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?