DEV Community

Cover image for A Beginner's Guide to Curl: Part 2 - Options
Jesse vB
Jesse vB

Posted on

A Beginner's Guide to Curl: Part 2 - Options

If you want to teach yourself how to use curl, the best resource is the manual.

Type the following command in your terminal.

$ man curl
Enter fullscreen mode Exit fullscreen mode

The man command is used to read the manual for other command line utilities (like curl). To exit the manual, just type q.

You should see a brief synopsis of what curl is and what it does. As you can see, there are a lot more protocols than just http. For this guide we will only be using http since that's what the internet is built off of.

If you scroll down in the manual, you'll see a section titled "OUTPUT". By default, curl will print the response to your STDOUT (standard output or terminal screen).

For example, exit the manual by typing q, and type the following in your terminal.

$ curl http://www.example.com
Enter fullscreen mode Exit fullscreen mode

You should see the HTML response printed below to your STDOUT. That's the default response when using curl. Now let's change that by adding an option.

Our first option will be the -o flag or its longer full version --output. Whenever you pass an option to curl, you can specify it with a short name (with one dash) or a full name (with two dashes).

The -o or --output option takes one argument, the name of the file you want to write to. This can be an existing file, or one that you haven't created yet.

Let's try it out! Type the following command. Notice we are changing our URL now.

$ curl --output three_sentences.txt http://metaphorpsum.com/sentences/3
Enter fullscreen mode Exit fullscreen mode

Two things just happened. First, curl created the file you specified in your current directory. Second, curl wrote the response from the sever to that file.

If you edit the file, you'll see the response is there. Another way to see the contents is to cat the file, you'll see the contents of the file printed to your STDOUT.

$ cat three_sentences.txt
Enter fullscreen mode Exit fullscreen mode

If you were to type the same command without the --output option and file name, what do you think would happen?

In the next article, we'll learn how to see more details contained in the request and response. A Beginner's Guide to Curl: Part 3

By the way, here are some other URLs you can try.

  • http://metaphorpsum/com/sentences/10
  • http://metaphorpsum/com/paragraphs/3
  • http://metaphorpsum/com/paragraphs/3/2

Top comments (0)