Always specify User-Agent in fetch

Cloudflare WorkersTIL

While performing audits it is a superpower to be able to fetch the website at Edge(e.g. Cloudflare Workers) and modify the HTML on the fly to check if a performance idea would work or not. But sometimes, a website can give 403. Usually, I think the website has some IP restrictions of a certain country, but that shouldn't be the case here as the Edge node would be very close to where the actual visitor is accessing the website.

The reason can sometimes very simply be not providing a proper User-Agent, which happened today with me while I scratched my head for a long time.

Always, specify User-Agent while doing a fetch

// A website that gives 404 if UA is not provided or a common browser UA isn't used.
fetch('https://example.com', {
    headers: {
      'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.106 Safari/537.36'
      }
    }
  );
... Loading comments