1 of 17

cURL

CS 240: Advanced Software Construction

2 of 17

cURL

Client for URLs

3 of 17

Why cURL?

  • Easy experimentation with HTTP endpoints
  • Available everywhere
  • Great for sharing with others
  • Great for debugging your endpoints
  • Generated from browser dev tools
  • Great for automation
  • Composable with other console commands

4 of 17

➜ curl byu.edu

5 of 17

➜ curl 'https://api.chucknorris.io/jokes/random?category=dev'

6 of 17

Parameters

Parameter

Purpose

Example

-X

Request HTTP method (GET is default)

-X POST

-v

Verbose. Display connection and headers

-v

-H

Provide a request HTTP header

-H “Content-Type: application/json”

-d

Request body data

-d ‘{“name”:”joe”}’

--data-binary

Request body data from file

--data-binary @req.json

-o

Output response body to file

-o response.json

-D

Dump response headers to file

-D headers.txt

7 of 17

➜ curl https://api.github.com/users/octocat -o octocat.json

8 of 17

➜ curl -v 'https://api.chucknorris.io/jokes/random?category=dev'

9 of 17

cURL for Chess

10 of 17

➜ curl localhost:8080/game

{"success":false,"message":"Error: Invalid auth token"}

11 of 17

➜ curl -X POST localhost:8080/user -d '{ "username": "bud", "password": "toomanysecrets", "email": "bud@byu.edu"}'

{"authToken":"607b0857","username":"bud"}

12 of 17

➜ curl -X POST localhost:8080/game -d '{ "gameName": "speed"}' -H "Authorization:607b0857"

{"gameID":1}

13 of 17

➜ curl localhost:8080/game -H "Authorization:607b0857"

{"games":[{"gameID":1,"gameName":"speed"}]}

14 of 17

Pro tips

  • When using Windows use curl.exe not curl
  • Use in conjunction with jq to format and query JSON
  • Automate the initialization of your database
    • -X delete /db
    • -X post /user
    • -X get /game
    • -X post /game
    • -X put /game

15 of 17

cURL Alternatives

16 of 17

Postman provides a graphical user interface

17 of 17

Postman VSCode Plugin