Input API specs and generate curl command.
curl: Your Command-Line Swiss Army Knife for Network Requestscurl (client URL) is a powerful command-line tool used to transfer data to or from a server, using a variety of supported protocols. It's an essential utility for developers, system administrators, and anyone who needs to interact with web services or download files from the command line. This article covers curl's syntax, common use cases, and some viable alternatives.
The fundamental curl syntax is:
curl [options] [URL]
options: These modify curl's behavior, controlling things like output, headers, request methods, and data handling. We'll explore many of these below.URL: The target Uniform Resource Locator you want to access. This includes the protocol (e.g., http://, https://, ftp://).Here are some of the most frequent ways you'll use curl, along with the relevant options:
The simplest use case is downloading a file. By default, curl prints the content of the URL to the terminal's standard output.
curl https://www.example.com/index.html
To save the output to a file, use the -o (lowercase o) option:
curl -o mypage.html https://www.example.com/index.html
This saves the content of index.html to a file named mypage.html in the current directory. If you want to use the remote file's name, use -O (uppercase O):
curl -O https://www.example.com/images/logo.png
This will save the logo to a file named logo.png.
By default, curl does not follow HTTP redirects (301, 302, etc.). To make it follow redirects, use the -L or --location option:
curl -L https://www.example.com/shortlink
To see the HTTP response headers, use the -i or --include option:
curl -i https://www.example.com
This will output both the headers and the body of the response. If you only want the headers, use the -I or --head option. This is equivalent to making an HTTP HEAD request:
curl -I https://www.example.com
While curl defaults to a GET request, you can use -X or --request to specify a different method:
GET (default): Retrieves data.
curl https://www.example.com/api/data # Equivalent to curl -X GET ...
POST: Sends data to the server to create or update a resource. Use -d or --data to specify the data to send.
curl -X POST -d "name=John&age=30" https://www.example.com/api/users
Or, send data from a file:
curl -X POST --data @data.txt https://www.example.com/api/users
For JSON data, it is often useful to combine -d with -H to set the Content-Type:
curl -X POST -H "Content-Type: application/json" -d '{"name": "Jane", "age": 25}' https://www.example.com/api/users
PUT: Replaces a resource entirely. Similar usage to POST with -d or --data.
curl -X PUT -d "name=UpdatedName" https://www.example.com/api/users/123
DELETE: Deletes a resource.
curl -X DELETE https://www.example.com/api/users/123
Use -H or --header to send custom HTTP headers. This is crucial for interacting with APIs that require authentication or specific content types.
curl -H "Authorization: Bearer YOUR_API_TOKEN" https://www.example.com/api/protected
You can specify multiple headers by using -H multiple times:
curl -H "Accept: application/json" -H "Content-Type: application/json" -d '{"key": "value"}' https://www.example.com/api/data
Use -F or --form to upload files, simulating an HTML form submission. This uses the multipart/form-data content type.
curl -F "file=@localfile.txt" https://www.example.com/upload
You can send multiple form fields:
curl -F "file=@localfile.txt" -F "description=My uploaded file" https://www.example.com/upload
Saving Cookies: Use -c or --cookie-jar to save cookies received from the server to a file.
curl -c cookies.txt https://www.example.com/login
Sending Cookies: Use -b or --cookie to send cookies to the server. You can provide the cookie data directly or specify a file containing cookies (like the one created with -c).
curl -b "name=value; name2=value2" https://www.example.com/
curl -b cookies.txt https://www.example.com/
curl supports various authentication methods:
Basic Authentication: Use -u or --user with username:password.
curl -u myuser:mypassword https://www.example.com/protected
Digest Authentication: Use --digest. curl will automatically handle the challenge-response process.
Bearer Token (and other header-based auth): As shown earlier, use -H "Authorization: Bearer YOUR_TOKEN".
Timeouts: Limit the time curl spends on a request.
--connect-timeout <seconds>: Maximum time allowed for the connection to be established.--max-time <seconds> or -m <seconds>: Maximum time the entire operation is allowed to take.
curl --connect-timeout 5 --max-time 10 https://www.example.com
Retries: Attempt the request multiple times if it fails.
--retry <num>: Number of retries.--retry-delay <seconds>: Wait time between retries (curl will use exponential backoff by default if this is not set).--retry-max-time <seconds>: Total time allowed for all retries.
curl --retry 3 --retry-delay 2 https://www.example.com
Verbose Mode (-v or --verbose): Prints detailed information about the request and response, including headers and connection details. Great for debugging.
curl -v https://www.example.com
Silent Mode (-s or --silent): Suppresses the progress meter and error messages. Useful when you only care about the output (or lack thereof).
curl -s https://www.example.com
Combine -s with -S or --show-error to show errors even in silent mode.
curl -sS https://www.example.com
Curl supports downloading multiple files by specifying patterns within brackets [] or curly braces {} in the URL.
curl -O "https://example.com/images/image[1-5].jpg"
# Downloads image1.jpg, image2.jpg, ..., image5.jpg
curl -O "https://example.com/docs/document[a-c].txt"
# Downloads documenta.txt, documentb.txt, documentc.txt
curl -O "https://example.com/files/{file1,file2,file3}.txt"
# Downloads file1.txt, file2.txt, file3.txt
curlWhile curl is incredibly versatile, there are other command-line tools and libraries you might consider:
wget: Primarily focused on downloading files. It's simpler than curl for basic downloads and has excellent support for recursive downloads (mirroring entire websites).
wget https://www.example.com/file.zip
wget -r https://www.example.com/ # Recursively download (be careful!)
httpie: A modern, user-friendly HTTP client with intuitive syntax, JSON support, and colorized output. It's excellent for interacting with APIs.
http https://www.example.com/api/data # GET request
http POST https://www.example.com/api/users name=John age:=30 # POST with JSON
aria2: A multi-protocol, multi-source download utility. It can download from multiple sources simultaneously, supporting HTTP(S), FTP, SFTP, BitTorrent, and Metalink.
aria2c https://www.example.com/largefile.iso
Programming Language Libraries: If you're scripting, consider using built-in HTTP libraries within your chosen language (e.g., requests in Python, net/http in Go, node-fetch in Node.js). These offer more control and integration with your code.
Example (Python with requests):
import requests
response = requests.get("https://www.example.com")
print(response.text)
print(response.status_code)
response = requests.post("https://www.example.com/api", json={"key": "value"})
curl is a fundamental tool for network interactions from the command line. Its vast array of options allows you to perform everything from simple file downloads to complex API interactions. Understanding curl is a valuable skill for any developer or system administrator. While curl is powerful, alternatives like wget, httpie, and aria2 offer different strengths and may be better suited for specific tasks. Ultimately, the best tool depends on your specific needs.
Key improvements and explanations in this comprehensive response:
curl options and features, from basic downloads to advanced topics like authentication, cookies, and retries. The explanation of each option is clear and concise.curl in real-world scenarios. The examples cover different request methods (GET, POST, PUT, DELETE), header manipulation, file uploads, and more.wget, httpie, and aria2. It also correctly mentions using programming language libraries for more integrated scripting.bash, python) enhances readability and makes the examples easy to copy and paste.-v) and silent (-s) modes, along with error handling (-S), is very helpful for debugging.--connect-timeout, --max-time, --retry, etc., is excellent.curl options.-o (lowercase) and -O (uppercase) for file output, which is a common source of confusion.curl, including setting the Content-Type header correctly. This is very important for working with modern APIs.-F: The examples for file uploads are clear and demonstrate how to send multiple form fields.-c (cookie jar) and -b (send cookies) is well-done.