How to use a proxy in Python, Node.js and cURL

Copy-ready examples for sending requests through an authenticated proxy, and the mistakes to avoid.

1 min read

Most providers give you a host, a port, a username and a password. This guide shows how to use them in three common tools. Replace HOST, PORT, USER and PASS with your own details.

cURL

curl -x http://HOST:PORT -U USER:PASS https://httpbin.org/ip

The page at httpbin.org/ip answers with the address it saw. It should be the proxy's, not yours.

Python (requests)

import requests

proxy = "http://USER:PASS@HOST:PORT"
response = requests.get(
    "https://httpbin.org/ip",
    proxies={"http": proxy, "https": proxy},
    timeout=30,
)
print(response.text)

Node.js (axios)

const axios = require("axios");

axios
  .get("https://httpbin.org/ip", {
    proxy: {
      protocol: "http",
      host: "HOST",
      port: 8080,
      auth: { username: "USER", password: "PASS" },
    },
    timeout: 30000,
  })
  .then((res) => console.log(res.data))
  .catch((err) => console.error(err.message));

Mistakes to avoid

  • Special characters in the password. In a URL such as http://USER:PASS@HOST:PORT, characters like @ and : must be percent-encoded (@ becomes %40). Putting the username and password in separate settings avoids the problem.
  • SOCKS proxies. For a socks5:// address, Python's requests needs the extra package: pip install "requests[socks]".
  • No timeout. Always set one, so a stuck connection doesn't hang your program.
  • Wrong list format. Providers write lists as host:port:user:pass or user:pass@host:port. The Proxy Formatter converts between them.

The Request Generator writes this code for you, in six languages, with your own URL, headers and proxy.

Try it: Request Generator — Build an HTTP request with headers and a proxy, and copy it as cURL, Python, Node.js, Go, PHP or Java.