User Agents

Web browsers and everyday web tech, explained

How to Use cURL User Agent to Get URL Source Code

· 5 min read · How the Web Works

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

This guide shows you how to use cURL with a custom User-Agent string to fetch the raw HTML source code of a web page. Setting a User-Agent makes your request look like it comes from a real browser, which many servers require before they'll send content. You'll learn the exact commands, how to save the output to a file, and how to troubleshoot common issues.

What You'll Need

  • A computer with cURL installed (macOS and Linux include it by default; Windows 10 and 11 include it in Command Prompt and PowerShell).
  • A terminal or command prompt window.
  • The URL of the page you want to fetch.
  • Basic familiarity with typing commands.

Step-by-Step

  1. Open your terminal. On macOS, use Terminal. On Linux, use any shell. On Windows, open Command Prompt or PowerShell.
  2. Test cURL is installed. Type curl --version and press Enter. You should see version information. If not, install cURL using your system's package manager or download it from the official website.
  3. Fetch the source code with a default User-Agent. Run curl https://example.com. This prints the raw HTML to your screen. However, many sites block requests without a browser-like User-Agent.
  4. Set a custom User-Agent. Use the -A flag followed by the User-Agent string. For example: curl -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" https://example.com. This makes your request look like it's from Chrome on Windows.
  5. Save the output to a file. Add -o filename.html to write the source code to a file instead of printing it. Example: curl -A "Mozilla/5.0" -o page.html https://example.com. The -o flag saves the body, while -O (capital O) saves it using the remote filename.
  6. Include headers if needed. Some servers check the Accept-Language or other headers. Use -H to add headers: curl -A "Mozilla/5.0" -H "Accept-Language: en-US,en;q=0.9" https://example.com.
  7. Follow redirects. If the page redirects (e.g., from HTTP to HTTPS), add -L to follow them: curl -L -A "Mozilla/5.0" https://example.com.
  8. View response headers. Add -i to include the HTTP response headers in the output, or -I to fetch only the headers. This helps debug server responses.
  9. Use a more realistic User-Agent. Copy the exact User-Agent string from your own browser (search "my user agent" in a search engine) to maximize compatibility. For example, on Firefox you might use Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:121.0) Gecko/20100101 Firefox/121.0.
  10. Check the file content. After saving, open the file in a text editor to verify it contains the expected HTML source code.

Common Mistakes

  • Forgetting to quote the User-Agent. The User-Agent string contains spaces, so always wrap it in double quotes. Without quotes, the shell splits it into multiple arguments.
  • Using -o vs -O incorrectly. Lowercase -o requires a filename; uppercase -O uses the remote filename. Mixing them up can cause errors.
  • Not following redirects. If you don't use -L, you might get a 301/302 response instead of the actual page content.
  • Using an outdated or generic User-Agent. Some sites block very old or obviously fake User-Agents. Use a current one from a popular browser.
  • Ignoring SSL errors. If a site has an invalid SSL certificate, cURL fails. Use -k to skip verification, but only for testing.

Tips

  • Test with a simple site first. Use https://example.com to verify your command works before trying complex pages.
  • Combine flags. You can combine -L -A -o in one command: curl -L -A "Mozilla/5.0" -o page.html https://example.com.
  • Use a file for headers. If you need many headers, create a file and use -H @headers.txt to include them all.
  • Check the response code. Add -w "%{http_code}" to print the HTTP status code after the transfer. A 200 means success.
  • Consider rate limiting. If you're fetching multiple pages, add a delay between requests to avoid being blocked. You can use sleep 1 in a loop.

Troubleshooting

I get a 403 Forbidden error. The server is rejecting your request. Try a more complete User-Agent, add an Accept header, or use --compressed to handle compressed responses.

The output is gibberish or binary. The server might be sending compressed data. Add --compressed to let cURL decompress it automatically.

I get an SSL certificate error. If you trust the site, use -k to skip verification. Otherwise, update your CA certificates.

The page loads in a browser but not via cURL. The site may be checking JavaScript or cookies. cURL doesn't execute JavaScript, so you'll only get the initial HTML. For dynamic content, consider using a headless browser.

cURL hangs or times out. Use --max-time 10 to set a timeout. Also check your network connection and firewall.

I get a 301/302 but no content. Add -L to follow redirects. If you still see issues, use -i to inspect the redirect location.

Frequently Asked Questions

What is a User-Agent string?

A User-Agent is a string that identifies your client (browser, bot, etc.) to the server. It typically includes the browser name, version, and operating system.

Why do I need to set a User-Agent?

Many websites block requests that don't have a browser-like User-Agent to prevent scraping or malicious traffic. Setting one makes your request appear normal.

Can I use any User-Agent?

Yes, you can use any string, but using a real browser's User-Agent is more likely to succeed. Some sites block obviously fake or empty User-Agents.

How do I get the source code of a page that requires login?

You'll need to include session cookies. First, log in using a browser and copy the cookies, then use -b "cookie_string" or -b cookies.txt to send them with cURL.

Does cURL execute JavaScript?

No, cURL only fetches the raw HTML. If the page content is generated by JavaScript, you'll need a tool like Puppeteer or Selenium.

How do I save the output to a file with a specific name?

Use -o filename.html. For example: curl -A "Mozilla/5.0" -o mypage.html https://example.com.