Question about using service bindings

Hi everyone!

My first post here. I am working on an a project, and I’m using Cloudflare Workers as the backend. To explain it simply, I have created 2 workers, one called “api” and the other called “logger”. I would like that whenever a request enters “api”, once the worker “api” is done processing the request, it will send all relevant logs about the request and what happened to the other worker called “logger”.

Now, both of these services are up and running, however when I first tried to connect them using fetch(loggerWorkUrl) I got an error 1042, which I searched up and seems to appear when you try to call another worker within a worker and that isn’t possible. So, I’m assuming it’s not possible to implement it any similar way to this?

So, I see Cloudflare has service bindings, which allow you to call other workers from the worker without any delays, however, but after reading all the pages, it seems like it’s only possible to use the original request (or a clone of it) to send to the service binding. Is there no way for me to send a “fake” request to the service binding with its own body, method and url?

Please let me know, and I appreciate all the help!

Hey @dropper

Have you checked out the Request documentation?

You can use a Request to fetch from the bound worker.

Here’s something I tried (successfully) which overrides the original request body, but retains all other information.

return await env.BINDING.fetch(new Request(req.url, {
  body: JSON.stringify({msg: "Ah ha!"}),
  method: req.method,
  headers: req.headers
}))