├── src ├── _curlrc ├── socks_proxy.sh ├── one_time_proxy.sh └── env_variables.sh ├── simple_proxy.sh └── README.md /src/_curlrc: -------------------------------------------------------------------------------- 1 | proxy="http://user:pwd@127.0.0.1:1234" -------------------------------------------------------------------------------- /src/socks_proxy.sh: -------------------------------------------------------------------------------- 1 | curl -x "socks5://user:pwd@127.0.0.1:1234" "http://httpbin.org/ip" -------------------------------------------------------------------------------- /simple_proxy.sh: -------------------------------------------------------------------------------- 1 | curl --proxy "http://user:pwd@127.0.0.1:1234" "http://httpbin.org/ip" 2 | # or 3 | curl --proxy "user:pwd@127.0.0.1:1234" "http://httpbin.org/ip" -------------------------------------------------------------------------------- /src/one_time_proxy.sh: -------------------------------------------------------------------------------- 1 | curl --proxy "http://user:pwd@1.0.0.1:8090" "http://httpbin.org/ip" 2 | # OR 3 | curl -x "http://user:pwd@1.0.0.1:8090" "http://httpbin.org/ip" -------------------------------------------------------------------------------- /src/env_variables.sh: -------------------------------------------------------------------------------- 1 | # Enable Proxy 2 | export http_proxy="http://user:pwd@127.0.0.1:1234" 3 | export https_proxy="http://user:pwd@127.0.0.1:1234" 4 | 5 | curl "http://httpbin.org/ip" 6 | 7 | # Disable proxy 8 | unset http_proxy 9 | unset https_proxy -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # How to Use cURL With Proxy 2 | 3 | [![Oxylabs promo code](https://raw.githubusercontent.com/oxylabs/product-integrations/refs/heads/master/Affiliate-Universal-1090x275.png)](https://oxylabs.io/pages/gitoxy?utm_source=877&utm_medium=affiliate&groupid=877&utm_content=curl-with-proxy-github&transaction_id=102f49063ab94276ae8f116d224b67) 4 | 5 | [![](https://dcbadge.limes.pink/api/server/Pds3gBmKMH?style=for-the-badge&theme=discord)](https://discord.gg/Pds3gBmKMH) [![YouTube](https://img.shields.io/badge/YouTube-Oxylabs-red?style=for-the-badge&logo=youtube&logoColor=white)](https://www.youtube.com/@oxylabs) 6 | 7 | [](https://github.com/topics/curl) [](https://github.com/topics/proxy) 8 | 9 | - [What is cURL?](#what-is-curl) 10 | - [Installation](#installation) 11 | - [What you need to connect to a proxy](#what-you-need-to-connect-to-a-proxy) 12 | - [Command line argument to set proxy in cURL](#command-line-argument-to-set-proxy-in-curl) 13 | - [Using environment variables](#using-environment-variables) 14 | - [Configure cURL to always use proxy](#configure-curl-to-always-use-proxy) 15 | - [Ignore or override proxy for one request](#ignore-or-override-proxy-for-one-request) 16 | - [Bonus tip – turning proxies off and on quickly](#bonus-tip--turning-proxies-off-and-on-quickly) 17 | - [cURL socks proxy](#curl-socks-proxy) 18 | 19 | This step-by-step guide will explain how to use cURL or, simply, curl with proxy servers. It covers all the aspects, beginning from installation to explaining various options to set the proxy. 20 | 21 | For a detailed explanation, see our [blog post](https://oxy.yt/ArRn). 22 | 23 | ## What is cURL? 24 | 25 | cURL is a command line tool for sending and receiving data using the URL. The following command gets the HTML of the page and prints it in the console: 26 | 27 | ```shell 28 | curl https://www.google.com 29 | ``` 30 | 31 | ## Installation 32 | 33 | cURL is provided with many Linux distributions and MacOS. Now, it is also provided with Windows 10. You can check whether your computer has curl installed by opening your terminal and running the following command: 34 | 35 | ```shell 36 | curl --version 37 | ``` 38 | 39 | ## What you need to connect to a proxy 40 | 41 | You’ll need the following information to anonymize curl behind a proxy: 42 | 43 | - proxy server address 44 | - port 45 | - protocol 46 | - username (if authentication is required) 47 | - password (if authentication is required) 48 | 49 | In this tutorial, let's assume that the proxy server is **127.0.0.1**, the port is **1234**, the user name is **user**, and the password is **pwd**. We will look into multiple examples covering various protocols. 50 | 51 | ## Command line argument to set proxy in cURL 52 | 53 | Open the terminal, type the following command, and press Enter: 54 | 55 | ```shell 56 | curl --help 57 | ``` 58 | 59 | The output is going to be a huge list of options. One of them is going to look like this: 60 | 61 | ```shell 62 | -x, --proxy [protocol://]host[:port] 63 | ``` 64 | 65 | Note that **x** is lowercase, and it is case-sensitive. The proxy details can be supplied using the **-x** or **–proxy** switch. Both mean the same thing: 66 | 67 | ```shell 68 | curl -x "http://user:pwd@127.0.0.1:1234" "http://httpbin.org/ip" 69 | ``` 70 | 71 | or 72 | 73 | ```shell 74 | curl --proxy "http://user:pwd@127.0.0.1:1234" "http://httpbin.org/ip" 75 | ``` 76 | 77 | **NOTE.** If there are SSL certificate errors, add **-k** (lowercase) to the **curl** command. This will allow insecure server connections when using SSL: 78 | 79 | ```shell 80 | curl --proxy "http://user:pwd@127.0.0.1:1234" "http://httpbin.org/ip" -k 81 | ``` 82 | 83 | Another interesting thing to note here is that the default proxy protocol is HTTP. Thus, the following two commands will do exactly the same: 84 | 85 | ```shell 86 | curl --proxy "http://user:pwd@127.0.0.1:1234" "http://httpbin.org/ip" 87 | curl --proxy "user:pwd@127.0.0.1:1234" "http://httpbin.org/ip" 88 | ``` 89 | 90 | ## Using environment variables 91 | 92 | Another way to use proxy with curl is to set the environment variables **http_proxy** and **https_proxy**: 93 | 94 | ```shell 95 | export http_proxy="http://user:pwd@127.0.0.1:1234" 96 | export https_proxy="http://user:pwd@127.0.0.1:1234" 97 | ``` 98 | 99 | After running these two commands, run **curl** normally: 100 | 101 | ```shell 102 | curl "http://httpbin.org/ip" 103 | ``` 104 | 105 | To stop using a proxy, turn off the global proxy by unsetting these two variables: 106 | 107 | ```shell 108 | unset http_proxy 109 | unset https_proxy 110 | ``` 111 | 112 | ## Configure cURL to always use a proxy 113 | 114 | If you want a proxy for curl but not for other programs, you can create a [curl config file](https://everything.curl.dev/cmdline/configfile.html). 115 | 116 | For Linux and MacOS, open the terminal and navigate to your home directory. If there is already a **.curlrc** file, open it. If there is none, create a new file. Here are the set of commands that can be run: 117 | 118 | ```shell 119 | cd ~ 120 | nano .curlrc 121 | ``` 122 | 123 | In this file, add a proxy authorization line: 124 | 125 | ```shell 126 | proxy="http://user:pwd@127.0.0.1:1234" 127 | ``` 128 | 129 | Save the file. Now, curl with proxy is ready to be used. 130 | 131 | Simply run **curl** normally and it will read the proxy from **.curlrc** file. 132 | 133 | ```shell 134 | curl "http://httpbin.org/ip" 135 | ``` 136 | 137 | On Windows, the file is named **_curlrc**. This file can be placed in the **%APPDATA%** directory. 138 | 139 | To find the exact path of **%APPDATA%**, open the command prompt and run the following command: 140 | 141 | ```shell 142 | echo %APPDATA% 143 | ``` 144 | 145 | This directory will be something like **C:\Users\\AppData\Roaming**. Now go to this directory, create a new file **_curlrc**, and set the proxy by adding this line: 146 | 147 | ```shell 148 | proxy="http://user:pwd@127.0.0.1:1234" 149 | ``` 150 | 151 | It works exactly the same way in Linux, MacOS, and Windows. 152 | 153 | ## Ignore or override proxy for one request 154 | 155 | To override the proxy for one request, set the new proxy using the **-x** or **–proxy** switch as usual: 156 | 157 | ```shell 158 | curl --proxy "http://user:pwd@1.0.0.1:8090" "http://httpbin.org/ip" 159 | ``` 160 | 161 | ## Bonus tip – turning proxies off and on quickly 162 | 163 | You can create an alias in your **.bashrc** file to set proxies and unset proxies. For example, open the **.bashrc** file using any editor and add these lines: 164 | 165 | ```shell 166 | alias proxyon="export http_proxy=' http://user:pwd@127.0.0.1:1234';export https_proxy=' http://user:pwd@127.0.0.1:1234'" 167 | alias proxyoff="unset http_proxy;unset https_proxy" 168 | ``` 169 | 170 | After adding the lines, save the **.bashrc** and update the shell to read this **.bashrc**. To do this, run this command in the terminal: 171 | 172 | ```shell 173 | . ~/.bashrc 174 | ``` 175 | 176 | Now, you can quickly turn on the proxy, run one or more curl commands, and then turn off the proxies like this: 177 | 178 | ```shell 179 | proxyon 180 | curl "http://httpbin.org/ip" 181 | curl "http://google.com" 182 | proxyoff 183 | ``` 184 | 185 | ## cURL SOCKS proxy 186 | 187 | If the proxy server is using the SOCKS protocol, the syntax remains the same: 188 | 189 | ```shell 190 | curl -x "socks5://user:pwd@127.0.0.1:1234" "http://httpbin.org/ip" 191 | ``` 192 | 193 | If you want to learn more about using cURL with proxy, see our [blog post](https://oxy.yt/ArRn). 194 | --------------------------------------------------------------------------------