How to Use cURL User Agent to Get URL Source Code
In this article
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
- Open your terminal. On macOS, use Terminal. On Linux, use any shell. On Windows, open Command Prompt or PowerShell.
- Test cURL is installed. Type
curl --versionand press Enter. You should see version information. If not, install cURL using your system's package manager or download it from the official website. - 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. - Set a custom User-Agent. Use the
-Aflag 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. - Save the output to a file. Add
-o filename.htmlto write the source code to a file instead of printing it. Example:curl -A "Mozilla/5.0" -o page.html https://example.com. The-oflag saves the body, while-O(capital O) saves it using the remote filename. - Include headers if needed. Some servers check the
Accept-Languageor other headers. Use-Hto add headers:curl -A "Mozilla/5.0" -H "Accept-Language: en-US,en;q=0.9" https://example.com. - Follow redirects. If the page redirects (e.g., from HTTP to HTTPS), add
-Lto follow them:curl -L -A "Mozilla/5.0" https://example.com. - View response headers. Add
-ito include the HTTP response headers in the output, or-Ito fetch only the headers. This helps debug server responses. - 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. - 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
-ovs-Oincorrectly. Lowercase-orequires a filename; uppercase-Ouses 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
-kto skip verification, but only for testing.
Tips
- Test with a simple site first. Use
https://example.comto verify your command works before trying complex pages. - Combine flags. You can combine
-L -A -oin 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.txtto 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 1in 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.