Understanding the curl -x Flag and Its Usage in Proxy Requests
Written on
Chapter 1: Introduction to the curl -x Flag
If you've found your way here, you might be curious about the function of the -x flag in a curl command you are examining. Let’s clarify its purpose with a practical example.
Consider the following command:
You can also achieve the same result using the longer form, --proxy, which is more explicit. Nonetheless, many users prefer the concise nature of the -x flag, which is why it is frequently used.
Below is an official definition from the curl manual:
Use the specified proxy.
The proxy string can be prefixed with a protocol:// format. If no protocol is given or if it is http://, the request will be treated as an HTTP proxy. To specify a particular SOCKS version, use socks4://, socks4a://, socks5://, or socks5h://.
Unix domain sockets are compatible for SOCKS proxies. For example, you can use socks5h://localhost/path/to/socket.sock.
If an unrecognized or unsupported proxy protocol is encountered, an error will be thrown since version 7.52.0. Prior versions may default to http://.
If no port number is provided in the proxy string, it defaults to 1080.
This option overrides any existing environment variables that set a proxy. You can nullify these settings by using proxy="".
All operations conducted over an HTTP proxy will be converted to HTTP, which may limit certain protocol-specific functions unless tunneling through the proxy using the --proxytunnel option.
User credentials included in the proxy string are URL decoded by curl, allowing special characters like @ (encoded as %40) or colons (encoded as %3a) to be passed in.
The proxy host may be specified similarly to environment variables, including the protocol prefix (http://) and embedded credentials.
If this option is invoked multiple times, the last one will take precedence.
Example:
Now that you are familiar with the -x flag, it shouldn’t seem too complicated, right?