cURL
CS 240: Advanced Software Construction
cURL
Client for URLs
Why cURL?
➜ curl byu.edu
➜ curl 'https://api.chucknorris.io/jokes/random?category=dev'
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 |
➜ curl https://api.github.com/users/octocat -o octocat.json
➜ curl -v 'https://api.chucknorris.io/jokes/random?category=dev'
cURL for Chess
➜ curl localhost:8080/game
{"success":false,"message":"Error: Invalid auth token"}
➜ curl -X POST localhost:8080/user -d '{ "username": "bud", "password": "toomanysecrets", "email": "bud@byu.edu"}'
{"authToken":"607b0857","username":"bud"}
➜ curl -X POST localhost:8080/game -d '{ "gameName": "speed"}' -H "Authorization:607b0857"
{"gameID":1}
➜ curl localhost:8080/game -H "Authorization:607b0857"
{"games":[{"gameID":1,"gameName":"speed"}]}
Pro tips
cURL Alternatives
Postman provides a graphical user interface
Postman VSCode Plugin