Tuesday, August 26, 2025

curl in Windows

Developers (on the Mac) has long used curl to test out http calls on the command line. Sure you can use the browser to test out any GET commmands, and use other things such as Postman to test POST. But curl is very handy and it is right on the command line. Now Windows users do not have it previously.. it was a separate download. If you have Windows 10 or 11 you have it pre-installed in your C:\windows\system32 folder. However, the world's example code and documentation are not so fair to developers on Windows. Flatty copy paste what you see will result in errors due to differences in Windows.

For example you are doing a little API with music albums your curl (for Mac) for inserting a record looks like this:

$ curl http://localhost:8080/albums \
    --include \
    --header "Content-Type: application/json" \
    --request "POST" \
    --data '{"id": "4","title": "The Modern Sound of Betty Carter","artist": "Betty Carter","price": 49.99}'
But boom not working for Windows because of the following.
  1. The not-yet-end-of-commmand character for Windows is not \, it is ^.
  2. Don't use single quotes. Json double quotes need to be escaped.

So the command above to run for above is

  curl http://localhost:8080/albums ^
    --include ^
    --header "Content-Type: application/json" ^
    --request "POST" ^
    --data "{\"id\": \"4\",\"title\": \"The Modern Sound of Betty Carter\",\"artist\": \"Betty Carter\",\"price\": 49.99}"
    

No comments: