How to Use Curl with an HTTP Proxy
This article explains how to route your curl
command-line requests through an HTTP proxy server. It covers the
essential command-line flags, environment variables, and authentication
methods needed to configure proxy settings for your web requests.
Using the Command Line Flag
The most direct way to route a single curl request
through an HTTP proxy is by using the -x or
--proxy option.
curl -x http://proxy.example.com:8080 https://api.example.comIn this command: * -x (or --proxy)
specifies the proxy server. * http://proxy.example.com:8080
is the address and port of your proxy. *
https://api.example.com is the destination URL.
Routing with Proxy Authentication
If your HTTP proxy requires a username and password, you can pass
these credentials using the -U (or
--proxy-user) option.
curl -U "username:password" -x http://proxy.example.com:8080 https://api.example.comAlternatively, you can embed the credentials directly into the proxy URL:
curl -x http://username:password@proxy.example.com:8080 https://api.example.comUsing Environment Variables
To avoid typing the proxy details for every command, you can set
environment variables in your terminal session. curl
automatically detects these variables.
For HTTP requests:
export http_proxy="http://proxy.example.com:8080"For HTTPS requests:
export https_proxy="http://proxy.example.com:8080"Once set, you can run curl normally, and the traffic
will route through the specified proxy:
curl https://api.example.comTo clear the proxy settings for your session, unset the variables:
unset http_proxy
unset https_proxySetting a Persistent Proxy via Configuration File
If you want to use a proxy permanently for all curl
requests without setting environment variables, you can add it to the
curl configuration file (~/.curlrc on macOS/Linux or
_curlrc on Windows).
Open or create the file:
nano ~/.curlrcAdd the following line:
proxy = "http://proxy.example.com:8080"
Save and close the file. Any future curl commands will
automatically route through this proxy.