A lightning talk about cURL
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

4.1 KiB

class: center,middle

cURL

brettlangdon | Shapeways

http://brettlangdon.github.io/curl-talk


What is cURL?

CLI tool for getting/sending files via URL's

Supported Protocols: .float-block[

  • DICT
  • FILE
  • FTP
  • FTPS
  • GOPHER
  • HTTP
  • HTTPS] .float-block[
  • IMAP
  • IMAPS
  • LDAP
  • LDAPS
  • POP3
  • POP3S
  • RTMP] .float-block[
  • RTSP
  • SCP
  • SFTP
  • SMTP
  • SMTPS
  • TELNET
  • TFTP]

Quick Examples

$ curl http://icanhazip.com
108.46.182.85

$ curl -i http://icanhazip.com
HTTP/1.1 200 OK
Date: Sat, 26 Apr 2014 19:49:57 GMT
Server: Apache
Content-Length: 14
Content-Type: text/plain; charset=UTF-8
Connection: close

108.46.182.85

Useful CLI Arguments

  • -v - Verbose: Make cURL more talkative
  • -h - Help: Show all CLI options
  • -F CONTENT - Form Data: Add Multipart Form data to request
  • -H HEADER - Header: Add HEADER to request header
  • -X COMMAND - Method: Set request method (GET/POST/etc)
  • -o FILE - Output: Output to FILE rather than stdout
  • -L - Follow Redirects: Follow all redirects
  • -i - Response Headers: Show the requests response headers

Download a File

Download to stdout

$ curl http://www.google.com
<!doctype html><html itemscope=""
itemtype="http://schema.org/WebPage"
lang="en"><head>
...

Save to a File "google-home.html"

$ curl -o google-home.html http://www.google.com

Response Headers Only

  • -I - Show response headers ONLY
$ curl -I http://www.google.com
HTTP/1.1 200 OK
Date: Sat, 26 Apr 2014 20:40:04 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=ISO-8859-1
Set-Cookie: PREF=ID=61629e9495553921:FF=0:TM=1398544804...
Set-Cookie: NID=67=HHSAgTCIZ3zd6w2hjrNqoX1VX9NDaqscV9Yc...
P3P: CP="This is not a P3P policy! See ..."
Server: gws
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Alternate-Protocol: 80:quic
Transfer-Encoding: chunked

Save/Reuse Cookies

  • -c FILE - Save cookies to FILE
  • -b FILE - Load cookies from FILE
$ curl -c cookies -b cookies http://www.google.com
$ cat cookies
# Netscape HTTP Cookie File
# http://curl.haxx.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.

ID=bec963a425f5d8ca:FF=0:TM=1398545024:LM=1398545024:S=...
67=BV0jhMokci-G3ZbOJ2UeFaNX1faFdfbFVcPHYIpAh35Uz2th6lVq...

Following Redirects

  • -I - Response headers only
  • -L - Follow redirects
$ curl -I -L http://google.com
HTTP/1.1 301 Moved Permanently
Location: http://www.google.com/
...
Expires: Sat, 31 May 2014 12:50:05 GMT
Cache-Control: public, max-age=2592000

HTTP/1.1 200 OK
Date: Thu, 01 May 2014 12:50:05 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=ISO-8859-1
...

HTTP POST Data

  • -X METHOD - Set HTTP Request Method
  • -d DATA - Send request form data
$ curl -X POST -d "name=brett&talk=curl" http://httpbin.org/post
{
  "form": {
    "name": "brett",
    "talk": "curl"
  },
  "headers": {
    "Host": "httpbin.org",
    "Content-Type": "application/x-www-form-urlencoded",
    ...
  },
  ...
}

HTTP POST File

  • -d @FILE - Send FILE with the request
  • -X METHOD - Set HTTP request method
  • -H HEADER - Add HEADER to request headers
$ cat data.json
{"name": "brett","talk": "curl"}
$ curl -X POST -d @data.json \
  -H "Content-Type: application/json" http://httpbin.org/post
{
  "data": "{\"name\": \"brett\",\"talk\": \"curl\"}",
  "json": {
    "talk": "curl",
    "name": "brett"
  },
  "headers": {
    "Content-Type": "application/json",
    ...
}

HTTPS

$ curl https://www.google.com
<!doctype html><html itemscope=""
itemtype="http://schema.org/WebPage"
lang="en"><head>
...

Allow Insecure Certs

  • -k - Allow Insecure Certs
$ curl -k https://insecure-site.gov

Learning More

$ man curl
$ curl --help