ZeroCold Posted January 31, 2014 Report Posted January 31, 2014 Working with HTTP from the command-line is a valuable skill for HTTP architects and API designers to have. The cURL library and curl command give you the ability to design a Request, put it on the pipe, and explore the Response. The downside to the power of curl is how much breadth its options cover. Running curl --help spits out 150 different flags and options. This article demonstrates nine basic, real-world applications of curl.In this tutorial we’ll use the httpkit echo service as our end point. The echo server’s Response is a JSON representation of the HTTP request it receives.Make a RequestLet’s start with the simplest curl command possible.Requestcurl http://echo.httpkit.comResponse{ "method": "GET", "uri": "/", "path": { "name": "/", "query": "", "params": {} }, "headers": { "host": "echo.httpkit.com", "user-agent": "curl/7.24.0 ...", "accept": "*/*" }, "body": null, "ip": "28.169.144.35", "powered-by": "http://httpkit.com", "docs": "http://httpkit.com/echo"}Just like that we have used curl to make an HTTP Request. The method, or “verb”, curl uses, by default, is GET. The resource, or “noun”, we are requestion is addressed by the URL pointing to the httpkit echo service, http://echo.httpkit.com.You can add path and query string parameters right to the URL.Requestcurl http://echo.httpkit.com/path?query=stringResponse{ ... "uri": "/path?query=string", "path": { "name": "/path", "query": "?query=string", "params": { "query": "string" } }, ...}Set the Request MethodThe curl default HTTP method, GET, can be set to any method you would like using the -X option. The usual suspects POST, PUT, DELETE, and even custom methods, can be specified.Requestcurl -X POST echo.httpkit.comResponse{ "method": "POST", ...}As you can see, the http:// protocol prefix can be dropped with curl because it is assumed by default. Let’s give DELETE a try, too.Requestcurl -X DELETE echo.httpkit.comResponse{ "method": "DELETE", ...}Set Request HeadersRequest headers allow clients to provide servers with meta information about things such as authorization, capabilities, and body content-type. OAuth2 uses an Authorization header to pass access tokens, for example. Custom headers are set in curl using the -H option.Requestcurl -H "Authorization: OAuth 2c4419d1aabeec" \ http://echo.httpkit.comResponse{..."headers": { "host": "echo.httpkit.com", "authorization": "OAuth 2c4419d1aabeec", ...},...}Multiple headers can be set by using the -H option multiple times.Requestcurl -H "Accept: application/json" \ -H "Authorization: OAuth 2c3455d1aeffc" \ http://echo.httpkit.comResponse{ ... "headers": { ... "host": "echo.httpkit.com", "accept": "application/json", "authorization": "OAuth 2c3455d1aeffc" }, ...}Send a Request BodyMany popular HTTP APIs today POST and PUT resources using application/json or application/xml rather than in an HTML form data. Let’s try PUTing some JSON data to the server.Requestcurl -X PUT \ -H 'Content-Type: application/json' \ -d '{"firstName":"Kris", "lastName":"Jordan"}' echo.httpkit.comResponse{ "method": "PUT", ... "headers": { ... "content-type": "application/json", "content-length": "40" }, "body": "{\"firstName\":\"Kris\",\"lastName\":\"Jordan\"}", ... }Use a File as a Request BodyEscaping JSON/XML at the command line can be a pain and sometimes the body payloads are large files. Luckily, cURL’s @readfile macro makes it easy to read in the contents of a file. If we had the above example’s JSON in a file named “example.json” we could have run it like this, instead:Requestcurl -X PUT \ -H 'Content-Type: application/json' \ -d @example.json echo.httpkit.comPOST HTML Form DataBeing able to set a custom method, like POST, is of little use if we can’t also send a request body with data. Perhaps we are testing the submission of an HTML form. Using the -d option we can specify URL encoded field names and values.Requestcurl -d "firstName=Kris" \ -d "lastName=Jordan" \ echo.httpkit.comResponse{ "method": "POST", ... "headers": { "content-length": "30", "content-type":"application/x-www-form-urlencoded" }, "body": "firstName=Kris&lastName=Jordan", ...}Notice the method is POST even though we did not specify it. When curl sees form field data it assumes POST. You can override the method using the -X flag discussed above. The “Content-Type” header is also automatically set to “application/x-www-form-urlencoded” so that the web server knows how to parse the content. Finally, the request body is composed by URL encoding each of the form fields.POST HTML Multipart / File FormsWhat about HTML forms with file uploads? As you know from writing HTML file upload form, these use a multipart/form-data Content-Type, with the enctype attribute in HTML. In cURL we can pair the -F option and the @readFile macro covered above.Requestcurl -F "firstName=Kris" \ -F "publicKey=@idrsa.pub;type=text/plain" \ echo.httpkit.comResponse{ "method": "POST", ... "headers": { "content-length": "697", "content-type": "multipart/form-data; boundary=----------------------------488327019409", ... }, "body": "------------------------------488327019409\r\n Content-Disposition: form-data; name=\"firstName\"\r\n\r\n Kris\r\n ------------------------------488327019409\r\n Content-Disposition: form-data; name=\"publicKey\"; filename=\"id_rsa.pub\"\r\n Content-Type: text/plain\r\n\r\n ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAkq1lZYUOJH2 ... more [a-zA-Z0-9]* ... naZXJw== krisjordan@gmail.com\n\r\n ------------------------------488327019409 --\r\n",...}Like with the -d flag, when using -F curl will automatically default to the POST method, the multipart/form-data content-type header, calculate length, and compose the multipart body for you. Notice how the @readFile macro will read the contents of a file into any string, it’s not just a standalone operator. The “;text/plain” specifies the MIME content-type of the file. Left unspecified, curl will attempt to sniff the content-type for you.Test Virtual Hosts, Avoid DNSTesting a virtual host or a caching proxy before modifying DNS and without overriding hosts is useful on occassion. With cURL just point the request at your host’s IP address and override the default Host header cURL sets up.Requestcurl -H "Host: google.com" 50.112.251.120Response{ "method": "GET", ... "headers": { "host": "google.com", ... }, ...}View Response HeadersAPIs are increasingly making use of response headers to provide information on authorization, rate limiting, caching, etc. With cURL you can view the headers and the body using the -i flag.Requestcurl -i echo.httpkit.com ResponseHTTP/1.1 200 OKServer: nginx/1.1.19Date: Wed, 29 Aug 2012 04:18:19 GMTContent-Type: application/json; charset=utf-8Content-Length: 391Connection: keep-aliveX-Powered-By: http://httpkit.com{ "method": "GET", "uri": "/", ...}# Sursa: 9 uses for cURL worth knowing | httpkit | Tools for hacking on HTTP 1 Quote