How To Use Proxy With cURL?

by mazen Mar 14, 2024

Proxies are essential to avoid IP address blocking and accessing restricted web pages over a specific location. In a nutshell, proxies are servers that allow for IP address hiding by requesting the web source through another, acting as a middleware.

To add proxy to cURL, we can use the -x or --proxy options. Let's explore using these options for proxy types.

HTTP and HTTPs Proxies

The general command for setting HTTP proxy with cURL is the following:

curl -x <protocol>://<proxy_host>:<proxy_port> <url>

The protocol indicates the HTTP protocol in the case of the HTTP proxies. It can be either htttp or http depending on the proxy type, as using the http protocol with a proxy that doesn't support it will result in errors. The proxy_host and proxy_port are configured based on the proxy itself. The proxy_host can be represented as a numeric IP address or domain name.

Here is an example of setting HTTP proxies with cURL. The actual proxy hostname and port are mocked:

# HTTP proxy
curl --proxy http://123.12.12.12:1234 http://example.com/
curl --proxy http://some_proxy_domain:1234 http://example.com/

# HTTPs proxy
curl --proxy http://123.12.12.12:1234 http://example.com/
curl --proxy http://some_proxy_domain:1234 http://example.com/

Authenticated Proxies

To proxy authentication with cURL, we only have to add the username and password as a prefix to the proxy. The same HTTP and HTTPs rules are also applied:

# HTTP proxy with authentication
curl --proxy http://username:password@123.12.12.12:1234 http://example.com/
curl --proxy http://username:password@some_proxy_domain:1234 http://example.com/

# HTTPs proxy with authentication
curl --proxy http://username:password@123.12.12.12:1234 http://example.com/
curl --proxy http://username:password@some_proxy_domain:1234 http://example.com/

SOCKS Proxies

The socks proxies operate over the SOCKS4 or the SOCKS5 protocol. Here is how to use socks proxies with cURL:

#SOCKS4
curl --proxy socks4://123.12.12.12:1234 http://example.com

#SOCKS5
curl --proxy socks5://123.12.12.12:1234 http://example.com

Related Articles

Advanced Proxy Connection Optimization Techniques

Master advanced proxy optimization with TCP connection pooling, TLS fingerprinting, DNS caching, and HTTP/2 multiplexing for maximum performance.

PROXIES
Advanced Proxy Connection Optimization Techniques

How to Stop Wasting Money on Proxies

Learn actionable techniques to slash proxy spending in web scraping projects without sacrificing reliability or scale.

PROXIES
How to Stop Wasting Money on Proxies

HTTPS vs. SOCKS Proxies

A deep dive into the key differences between HTTPS and SOCKS proxies, helping you choose the right protocol for your web scraping needs.

PROXIES
HTTPS vs. SOCKS Proxies

Optimize Proxy Bandwidth with Image & CSS Stubbing

Reduce proxy costs by 30-50% through intelligent image and CSS stubbing techniques that eliminate unnecessary resource downloads while preserving functionality.

PROXIES
Optimize Proxy Bandwidth with Image & CSS Stubbing

What Is a Proxy Server?

Understand what proxy servers are, how they work, and how developers can leverage them for efficient, large-scale web scraping.

PROXIES
What Is a Proxy Server?

What is a Reverse Proxy?

Learn what reverse proxies are, how they differ from forward proxies, and their critical role in web infrastructure and web scraping. A deep dive for developers.

PROXIES
What is a Reverse Proxy?