Is it possible to invoke API for a private Worker using HTTP Service bindings?

Is it possible to make arbitrary API requests from the frontend to the backend using the worker's HTTP Service Bindings? In a typical use case, it is known that a request to a worker can be forwarded to another worker. It is also stated that arbitrary requests can be made by providing a fully qualified URL as shown below. via https://developers.cloudflare.com/workers/runtime-apis/bindings/service-bindings/http/
export default {
async fetch(request, env) {
// provide a valid URL
let newRequest = new Request("https://valid-url.com", { method: "GET" });
let response = await env.WORKER_B.fetch(newRequest);
return response;
}
};
export default {
async fetch(request, env) {
// provide a valid URL
let newRequest = new Request("https://valid-url.com", { method: "GET" });
let response = await env.WORKER_B.fetch(newRequest);
return response;
}
};
If let workers_dev=false on WORKER_B and hide the hostname, can we use fetch to call any API on WORKER_B? Or should we use RPC for this use case? Assumed usercase:
sequenceDiagram
User ->> Frontend (WORKER_A): GET /
Frontend (WORKER_A) ->> Backend (WORKER_B): GET /api/user
sequenceDiagram
User ->> Frontend (WORKER_A): GET /
Frontend (WORKER_A) ->> Backend (WORKER_B): GET /api/user
Cloudflare Docs
HTTP
Facilitate Worker-to-Worker communication by forwarding Request objects.
1 Reply
ryok
ryokOP3w ago
Just in the case for a local env, creating a Request object as shown below worked as expected.
let newRequest = new Request(new URL("/", request.url).href, { method: "GET" });
let newRequest = new Request(new URL("/", request.url).href, { method: "GET" });
So, I will close this thread as resolved.

Did you find this page helpful?