The curl command-line tool for transferring data using various network protocols. The most common use case for web developers is testing REST APIs.
In some environments, especially corporate networks, all HTTP traffic is routed through a proxy server.
In this tutorial, we’ll learn a few ways to set up curl to permanently use a proxy server of our choice.
2. Using a Proxy
First, let’s see how we tell curl to use a proxy server. Assume we want to access the Baeldung website through a proxy running on our local host, port 8080. We do this by executing the following statement:
$ curl --proxy http://127.0.0.1:8080 https://baeldung.com
3. Create an Alias
A simple way to make curl use our proxy permanently would be to create an alias. Let’s append this line to our ~/.bashrc file:
$ alias curl="curl -x http://127.0.0.1:8080"
Now, after reloading our shell, running curl will invoke our alias and implicitly use the proxy server:
$ curl https://baeldung.com
In this example, we use the rc (run commands) file that is specific to Bash. Other shells have their own rc files. For example, the Z shell uses a file called ~/.zshrc. We could also choose to define the alias in our shell profile.
4. Using Environment Variables
Another way to tell curl to use our proxy server is by using the environment variable called http_proxy. We can set this variable using the export command:
$ export http_proxy=http://127.0.0.1:8080
We make this permanent by adding the variable to our shell profile. For example, in bash, we add it to our ~/.profile like this:
5. Using .curlrc
While the solutions above all work, curl actually has its own way of setting default options. On startup, curl will look for a ~/.curlrc file. We can append this line to ~/.curlrc to make it permanently use our proxy:
Should ~/.curlrc not already exist, we simply create a new file.
In this article, we learned how to make curl permanently send all requests through a proxy server. While there are several ways to do this, using a .curlrc file in our home directory is recommended.