Search

11/11/2012

9 uses for cURL worth knowing

9 uses for cURL worth knowing
http://echo.httpkit.com

curl http://echo.httpkit.com

Set the Request Method

curl -X POST echo.httpkit.com

Set Request Headers

curl -H "Authorization: OAuth 2c4419d1aabeec" \
     http://echo.httpkit.com
curl -H "Accept: application/json" \
     -H "Authorization: OAuth 2c3455d1aeffc" \
     http://echo.httpkit.com

Send a Request Body

Many 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.
curl -X PUT \
     -H 'Content-Type: application/json' \
     -d '{"firstName":"Kris", "lastName":"Jordan"}'
     echo.httpkit.com

Use a File as a Request Body

Escaping 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:
curl -X PUT \
     -H 'Content-Type: application/json' \
     -d @example.json
     echo.httpkit.com

POST HTML Form Data

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.
curl -d "firstName=Kris" \
     -d "lastName=Jordan" \
     echo.httpkit.com

POST HTML Multipart / File Forms

What 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. 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.
curl -F "firstName=Kris" \
     -F "publicKey=@idrsa.pub;type=text/plain" \
     echo.httpkit.com
{
  "method": "POST",
  "uri": "/",
  "path": {
    "name": "/",
    "query": "",
    "params": {}
  },
  "headers": {
    "x-forwarded-for": "1.34.59.62",
    "host": "echo.httpkit.com",
    "user-agent": "curl/7.19.7 (universal-apple-darwin10.0) libcurl/7.19.7 OpenSSL/0.9.8r zlib/1.2.3",
    "accept": "*/*",
    "content-length": "307",
    "content-type": "multipart/form-data; boundary=----------------------------c8fa62213588"
  },
  "body": "------------------------------c8fa62213588\r\nContent-Disposition: form-data; name=\"firstName\"\r\n\r\nKris\r\n------------------------------c8fa62213588\r\nContent-Disposition: form-data; name=\"publicKey\"; filename=\"idrsa.pub\"\r\nContent-Type: text/plain\r\n\r\n\nhello world\n\r\n------------------------------c8fa62213588--\r\n",
  "ip": "127.0.0.1",
  "powered-by": "http://httpkit.com",
  "docs": "http://httpkit.com/echo"

沒有留言: