🖥️ Slides
Curl (technically cURL
) is essentially a command line web browser. It allows you to see, debug, and execute HTTP requests from the command line console window of any operating system. This makes Curl a valuable tool for any software engineer.
The most basic syntax of curl is to simply provide a URL to the Curl application.
➜ curl byu.edu
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="https://www.byu.edu/">here</a>.</p>
</body></html>
This will return the HTML page located at the URL. You can see the details of the HTTP connection and request by including the -v
parameter. This is helpful for debugging HTTP headers and security interactions.
➜ curl -v byu.edu
* Connected to byu.edu (128.187.16.184) port 80 (#0)
> GET / HTTP/1.1
> Host: byu.edu
> User-Agent: curl/7.87.0
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Date: Thu, 03 Aug 2023 16:52:23 GMT
< Server: Apache/2.5.37 (Red Hat Enterprise Linux) OpenSSL/1.1.1k
< Location: https://www.byu.edu/
< Expires: Thu, 03 Aug 2023 17:52:23 GMT
< Content-Length: 228
< Content-Type: text/html; charset=iso-8859-1
<
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
...
If you want to make a Curl request that explicitly provides the HTTP method, headers, or body, you can use the following syntax.
Purpose | Syntax | Example |
---|---|---|
Method | -X method | -X PUT |
Header | -H header | -H "content-type:application/json" |
Body | -d body | -d '{"name":"berners-lee"}' |
Putting this all together in a single request would look like the following if you were trying to create a new chess game using a server running locally.
➜ curl -X POST localhost:8080/game -d '{ "gameName": "speed"}' -H "Authorization:607b0857"
Take some time to get familiar with Curl. You can use it to test your server, programmatically make batch requests, or to probe and debug how other web services work.
Note
Note that when running under Windows Powershell curl
is mapped to the Invoke-WebRequest
command. You don't want to use that. Instead type curl.exe
to actually access Curl.