Curl Cheat Sheet

In curl

Curl is a command-line tool used for transferring data from or to a server. It supports various protocols such as HTTP, FTP, SMTP, POP3, IMAP, and many more. Curl is widely used by developers and system administrators for testing APIs, downloading files, and automating tasks.

One of the key features of curl is its ability to send HTTP requests with various methods such as GET, POST, PUT, DELETE, and more. It also supports authentication, cookies, and SSL/TLS encryption. Curl can be used to test REST APIs by sending requests and receiving responses in JSON or XML format.

Curl is available on most operating systems including Windows, macOS, and Linux. It can be installed using package managers such as Homebrew, apt-get, or yum. Once installed, curl can be used from the command line by typing “curl” followed by the URL or IP address of the server.

In addition to its command-line interface, curl also has a library called libcurl that can be used in various programming languages such as C, Python, and PHP. This library provides a simple API for sending HTTP requests and receiving responses.

Overall, curl is a powerful tool for developers and system administrators that can simplify tasks such as testing APIs, downloading files, and automating tasks. Its versatility and ease of use make it a popular choice for many developers.

Cheat Sheet

Basic Usage
`curl [URL]`Sends a GET request to the specified URL
`curl -X [HTTP_METHOD] [URL]`Sends a request with the specified HTTP method
`curl -d [DATA] [URL]`Sends a POST request with the specified data
`curl -H [HEADER] [URL]`Sends a request with the specified header
`curl -i [URL]`Sends a request and displays the response headers
`curl -v [URL]`Sends a request and displays verbose output
Request Methods
`-X GET`Sends a GET request
`-X POST`Sends a POST request
`-X PUT`Sends a PUT request
`-X DELETE`Sends a DELETE request
`-X HEAD`Sends a HEAD request
`-X OPTIONS`Sends an OPTIONS request
Request Headers
`-H “Content-Type: application/json”`Sets the content type header to JSON
`-H “Authorization: Bearer [TOKEN]”`Sets the authorization header with a token
`-H “User-Agent: [AGENT]”`Sets the user agent header
`-H “Accept: [MIME_TYPE]”`Sets the accept header with a MIME type
`-H “Referer: [URL]”`Sets the referer header with a URL
Request Data
`-d “param1=value1&param2=value2″`Sends data as a URL-encoded string
`-d “@[FILE_PATH]”`Sends data from a file
`-d ‘{“key1″:”value1″,”key2″:”value2”}’`Sends data as JSON
`-d “@-“`Reads data from stdin
Response Handling
`-o [FILE_PATH]`Saves the response to a file
`-O`Saves the response to a file with the same name as the URL
`-s`Silent mode, hides progress meter and error messages
`-w “%{http_code}”`Displays only the HTTP status code
`-I`Sends a HEAD request and displays only the response headers
`-u [USERNAME:PASSWORD]`Sets basic authentication credentials
`–ntlm`Enables NTLM authentication
`–negotiate`Enables Negotiate authentication
`–digest`Enables Digest authentication
`-k`Allows insecure SSL connections
`–cacert [CERT_FILE]`Uses the specified CA certificate
`–cert [CERT_FILE]`Uses the specified client certificate
`–key [KEY_FILE]`Uses the specified client key
`–capath [DIR_PATH]`Uses the specified directory of CA certificates
`-c [COOKIE_FILE]`Uses the specified cookie file for input
`-b [COOKIE_FILE]`Uses the specified cookie file for output
`-j`Enables the cookie jar for persistent cookies
`-L`Follows redirects and sends cookies
`–trace [FILE_PATH]`Logs the entire request and response to a file
`–trace-ascii [FILE_PATH]`Logs the ASCII version of the request and response to a file
`–trace-time`Adds timestamps to the trace output
`–verbose`Displays verbose output
`-A [AGENT]`Sets the user agent
`-e [REFERER]`Sets the referer
`-G`Sends a GET request with query parameters
`-H “Expect:”`Disables the “Expect: 100-continue” header
`-h`Displays the help message
`-V`Displays the version information