Making HTTP requests to IP address from Worker

I am trying to make HTTP requests from my Cloudflare Worker to a backend service that is only available via IP address (no domain name): The same call works over CURL, but from my Worker I get a 1003 Error. - I tried using fetch and get blocked - I tried using Node's http module with nodejs_compat and getting: [unenv] http.request is not implemented yet!" - When I try to make the call directly from the Frontend (my html file), I get a mixed content error because my site is served over https but the backend is http. Question: What's the recommended way to make HTTP requests to an IP address from a Worker when a domain name isn't available? Is there a way to bypass the IP address restriction, or should I be looking at a different architecture?
app.post("/api/orders", async (c) => {
try {
const body = await c.req.json();
const response = await fetch("http://xx.xxx.xx.xxx/orders", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body)
});
// ... handle response
} catch (error) {
return c.json({ error: 'Failed to process order' }, 500);
}
});
app.post("/api/orders", async (c) => {
try {
const body = await c.req.json();
const response = await fetch("http://xx.xxx.xx.xxx/orders", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body)
});
// ... handle response
} catch (error) {
return c.json({ error: 'Failed to process order' }, 500);
}
});
3 Replies
Chaika
Chaika2w ago
You can't directly fetch an IP Address from Workers, for security reasons fetching insecure/plaintext http across the internet is a really bad idea anyway
Chaika
Chaika2w ago
Cloudflare Tunnels are an easy way to securely connect to an origin w/o configuring anything on the origin https wise, they're just a piece of software which runs on the origin server and connects securely back out to Cloudflare https://developers.cloudflare.com/cloudflare-one/connections/connect-networks/get-started/create-remote-tunnel/
Cloudflare Docs
Create a tunnel (dashboard)
Follow this step-by-step guide to create your first remotely-managed tunnel using Zero Trust.
Nlea
NleaOP2w ago
Thanks for clarifying that it isn't possible. I know it is not good practice but I'm looking into this for a demo/prototype and I currently don't have access to the server hosting the IP address. So I can't implement a tunnel there.

Did you find this page helpful?