584

I have this proxy address: 125.119.175.48:8909

How can I perform a HTTP request using cURL like curl http://www.example.com, but specifying the proxy address of my network?

ivanleoncz
  • 9,070
  • 7
  • 57
  • 49
user873286
  • 7,799
  • 7
  • 30
  • 38

18 Answers18

796

From man curl:

-x, --proxy <[protocol://][user:password@]proxyhost[:port]>

     Use the specified HTTP proxy. 
     If the port number is not specified, it is assumed at port 1080.
Ben G
  • 26,091
  • 34
  • 103
  • 170
Karl Barker
  • 11,095
  • 3
  • 21
  • 26
  • curl: (7) couldn't connect to host curl: (7) couldn't connect to host – user873286 Feb 27 '12 at 22:50
  • I get above error when I run this command: curl -x, --proxy 122.72.2.200:80 http://www.mysite.com/test.php?id=1 – user873286 Feb 27 '12 at 22:52
  • 83
    "-x, --proxy" means that either one can be used. The command should be either "curl -x 122.72.2.200:80 http://mysite.com/test.php?id=1" or "curl --proxy 122.72.2.200:80 http://mysite.com/test.php?id=1" – IBBoard Dec 04 '12 at 11:39
  • 10
    The docs are correctly quoted as above, however, the user and password should be expressed differently: `-x user:password@proxyhost:port` – Ed Chapel Sep 07 '13 at 12:14
  • 1
    if you need to curl and get via proxy do `curl -x GET mysite.com --proxy yourproxy:port` – Ray Sep 06 '17 at 03:48
499

General way:

export http_proxy=http://your.proxy.server:port/

Then you can connect through proxy from (many) application.

And, as per comment below, for https:

export https_proxy=https://your.proxy.server:port/
airween
  • 6,203
  • 1
  • 14
  • 20
  • 81
    When you need to proxy HTTPS traffic, the environment variable is upper case: HTTPS_PROXY – phatblat Nov 14 '12 at 16:38
  • 26
    From the [man pages](http://curl.haxx.se/docs/manpage.html) The environment variables can be specified in lower case or upper case. The lower case version has precedence. http_proxy is an exception as it is only available in lower case. Using an environment variable to set the proxy has the same effect as using the --proxy option. – Captain Lepton Jun 19 '13 at 14:23
  • @BipinVayalu, what do you want to check? At this way, what I describe above, the client uses the proxy "direct way", not transparently. So, the proxy gives the standard HTTP answers (including HTTP codes). If you export a wrong http_proxy environment variable (eg. bad port...), the client could't connet to that, and it gives an error message: "Warning! Couldn't connect to remote host!", or something similar - just try it :) – airween May 23 '14 at 09:18
  • 17
    If you check curl source code, you will see at url.c(line 4337 in verion 7.39) they check for lowercase version and if they can't find it, check for the uppercase. – Jose Palma Nov 24 '14 at 14:17
  • 1
    Most users sent me a correction, now I share that. If you want to use HTTPS connection through proxy, then you should do this: export https_proxy=http://your.proxy.server:port/ Note, that there is "http" proto in URL schema, not httpS! – airween Aug 23 '15 at 18:59
  • Please note: https://blog.emacsos.com/use-socks5-proxy-in-curl.html . libcurl honours ALL_PROXY, HTTPS_PROXY and http_proxy vars. – socketpair Jun 17 '19 at 08:28
  • May be in some cases the uppercase format of the environment variable is expected, but the right form of the answer for this question is the lowercase format. – airween Jun 19 '19 at 14:07
  • Basicaly this is right but for general the question was related to curl - so @Karl Barker ' s answer should be selected – itshorty Aug 08 '19 at 14:47
  • This is not a general way, it is Linux specific – Alek Depler Oct 21 '21 at 12:03
  • ok, so the https_proxy does not need to be accessed via https. For example: export https_proxy="http: //your.proxy.server:8080" would allow you to do curl https: //the.website.com (remove the space between the http: and the //, and https: and the //). I also found out in the curl man page that you can use variable ALL_PROXY instead of having to use the 2 variables http_proxy and https_proxy: "Sets the proxy server to use if no protocol-specific proxy is specified" – Stephane Desmarais Nov 16 '21 at 16:19
  • @StephaneDesmarais please see the comments above: you don't need to write the variables with uppercase, and the lowercase form has precedence (this is for your proposal for my answer). And yes, you don't need to define the schema as https - well, you can't do that, except your proxy supports the https. – airween Nov 16 '21 at 19:58
  • @ariween yeah, I found after proposing the change about lowercase and uppercase as you explain, and tried to undo my change. Thanks – Stephane Desmarais Nov 24 '21 at 19:43
  • @StephaneDesmarais yw :). – airween Nov 25 '21 at 07:12
165

The above solutions might not work with some curl versions I tried them for myself(curl 7.22.0). But what worked for me was:

curl -x http://proxy_server:proxy_port --proxy-user username:password -L http://url

Hope it solves the issue better!

Amar
  • 2,171
  • 1
  • 13
  • 16
107

Beware that if you are using a SOCKS proxy, instead of a HTTP/HTTPS proxy, you will need to use the --socks5 switch instead:

curl --socks5 125.119.175.48:8909 http://example.com/

You can also use --socks5-hostname instead of --socks5 to resolve DNS on the proxy side.

Filipe Correia
  • 5,415
  • 6
  • 32
  • 47
  • 9
    This didn't work for me, but using `--socks5-hostname` did. – Michał Rybak Apr 14 '16 at 19:28
  • 2
    @MichałRybak that will work the same but resolve DNS on the proxy side. See the [man page](https://curl.haxx.se/docs/manpage.html) for more details. – Filipe Correia Apr 15 '16 at 09:32
  • 3
    `curl https://api.ipify.org --proxy socks5://189.100.246.182:37339` seems to work fine for me. Is the `--proxy` option in some way inferior to `--socks5` when using socks5 proxies? –  Jul 05 '17 at 12:38
  • @user993683 It's version-dependent. `--proxy` is probably what you want, now, but it wasn't always supported. Also note that `socks5://` and `socks5h://` perform hostname (DNS) resolution before and after connecting to the proxy, respectively. – Michael May 09 '19 at 16:23
61

as an adition to airween, another good idea is to add this into your .bashrc, so you'll be able to switch from non proxied to proxied environment:

alias proxyon="export http_proxy='http://YOURPROXY:YOURPORT';export https_proxy='http://YOURPROXY:YOURPORT'"
alias proxyoff="export http_proxy='';export https_proxy=''"

WHERE YOURPROXY:YOURPORT is exactly that, your ip and port proxy :-).

Then, simply doing

proxyon

your system will start to use the proxy, and just the opposite with:

proxyoff
Alejandro Moreno
  • 5,578
  • 2
  • 31
  • 29
41

use the following

curl -I -x 192.168.X.X:XX http://google.com

192.168.X.X:XX put your proxy server ip and port.

-v verbose mode it will give more details including headers and response.

Community
  • 1
  • 1
13krn
  • 541
  • 4
  • 9
  • 2
    This should be the answer. Not everyone wants to specify a global variable (export) for every HTTP request performed by every HTTP client on your host. This option gives more flexibility, IMHO. – ivanleoncz Aug 22 '17 at 15:44
  • This is so useful when you're trying different proxies against an url. Thanks. – redAce Oct 02 '18 at 13:51
30

I like using this in order to get the IP under which I am seen

curl -x http://proxy_server:proxy_port https://api.ipify.org?format=json && echo

Hope this helps someone.

bmetge
  • 341
  • 3
  • 4
27

For curl you can configure proxy in your ~/.curlrc (_curlrc on Windows) file by adding proxy value, the syntax is:

proxy = http://username:password@proxy-host:port
kenorb
  • 155,785
  • 88
  • 678
  • 743
  • That's it! Configured the proxy setting some years ago in that file and wondered today, why curl doesn't work anymore now the proxy adress changed and the env vars are all set correct. Thanks for that! – emale Mar 19 '19 at 09:29
  • On Windows, the "_curlrc" file can (must?) be placed into the "%APPDATA%" directory. – Fred Danna Oct 01 '21 at 16:03
25

curl -I "https://www.google.com" -x 1.1.1.1:8080

evandrix
  • 6,041
  • 4
  • 27
  • 38
Debashish Saha
  • 259
  • 3
  • 2
16

Just summarizing all great mentioned answers:

curl -x http://<user>:<pass>@<proxyhost>:<port>/ -o <filename> -L <link>
Morteza Mashayekhi
  • 934
  • 11
  • 23
10

With a proxy with authentication I use:

curl -x <protocol>://<user>:<password>@<host>:<port> --proxy-anyauth <url>

because, I don't know why curl doesn't use/catch http[s]_proxy environment variables.

5

You don't need to export the http[s]_proxy shell variable if you're just setting the proxy for a one off command. e.g.

http_proxy=http://your.proxy.server:port curl http://www.example.com

That said, I'd prefer curl -x if I knew I was always going to use a proxy.

overthink
  • 23,985
  • 4
  • 69
  • 69
4
sudo curl -x http://10.1.1.50:8080/ -fsSL https://download.docker.com/linux/ubuntu/gpg

This worked perfectly for me, the error comes because curl need to set the proxy

Remmember replace the proxy with your proxy, mine, "example" was http://10.1.1.50:8080/.

IbarMario
  • 51
  • 2
4

curl -vv -ksL "https://example.com" -x "http://<proxy>:<port>"

evandrix
  • 6,041
  • 4
  • 27
  • 38
iamtheexp01
  • 3,446
  • 9
  • 35
  • 35
2

Depending on your workplace, you may also need to specify the -k or the --insecure option for curl in order to get past potential issues with CA certificates.

curl -x <myCompanyProxy>:<port> -k -O -L <link to file to download>
1

For http proxy tunnels (needed for the TLS protocol), you need to specify -p (aka --proxytunnel) instead of -x.

curl post about proxies

tl;dr the proxy tunnel uses a newer "CONNECT" keyword instead of a modified "GET"

This was needed for the node http-proxy-middleware library.

Only got a clue once I used wget which worked out of the box.

Gerard ONeill
  • 3,914
  • 39
  • 25
0

In case the proxy is using automatic proxy with PAC file. We can find the actual proxy from the javascript from the PAC URL.

And if the proxy needs authentication, we can first use a normal web-browser to access the website which will promote authentication dialog. After authentication, we can use wireshark to capture the http package sends to the proxy server, from the http package, we can get the auth token from http header: Proxy-Authorization

Then we can set the http_proxy environment variable and also include auth token in the http header: Proxy-Authorization

export http_proxy=http://proxyserver:port

curl -H "Proxy-Authorization: xxxx" http://targetURL

Jianwu Chen
  • 5,336
  • 3
  • 30
  • 35
0
curl -x socks5://username:password@ip:port example.com
S.B
  • 13,077
  • 10
  • 22
  • 49