Accessing Cloudflare API from localhost

Hello, I’m trying to create cloudflare media library module for Netlify CMS similar to Cloudinary media library (Cloudinary | Netlify CMS | Open-Source Content Management System). For that I need to use Cloudflare API to be able to list uploaded images and upload new images from the CMS interface. But I can’t figure out how to access Cloudflare API from the localhost. I added simple proxy to my dev server:

app.use(
      '/cloudflare',
      createProxyMiddleware({
        target: 'https://api.cloudflare.com',
        changeOrigin: true,
      }),
    )

but when I’m trying to send request to http://localhost:8000/cloudflare/client/v4/accounts/:account_id/images/v1, I get 404 page in response. And there is only ‘cloudflare-lb’ text on this page. Any ideas what’s going on here?

I’m guessing that /cloudflare is appended to the path when sending the request to the Cloudflare API. Seems like changeOrigin takes care of changing the HTTP Host header, so that’s good, but the path might be incorrect. A quick Google search tells me “prependPath” and “ignorePath” might be of interest. Maybe you can set to false, true, and add a path to the target? Just thinking out loud :smiley:

1 Like

Thank you very much, you were absolutely right. ‘/cloudflare’ has been added to resulting path. This did the trick

app.use(
      '/cloudflare',
      createProxyMiddleware({
        target: 'https://api.cloudflare.com',
        changeOrigin: true,
        pathRewrite: { '^/cloudflare': '' },
      }),
    )

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.