Skip to main content

Command Palette

Search for a command to run...

Getting Started with cURL

Published
4 min readView as Markdown

A server is a librarian who holds information that you want. Now you want to get the information located at https://www.google.com , and you don’t have a browser to execute the search. What will you do? You will take the help of the Command Line Interface to solve your problem.

What is cURL

cURL is a command-line tool that you execute in a terminal to fetch information when you don’t want to use a browser. It does the same work but without any skin(browsers) or any fancy buttons. Consider browser is for humans and cURL is for Developers. cURL is ‘Client URL’ means that your computer(Client) goes to a URL. It is basically accessing a site or a server having some data on a terminal and not on a browser, where css makes it beautiful, and JS gives it logic. You receive pure HTML in response to your request.

Making your first request using cURL

Now we will use cURL and understand how it works and what response do we get.

curl https://www.google.com

Executing this will send a request to google.com server and ask for a response

StatusCode        : 200
StatusDescription : OK
Content           : <!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="en-IN"><head><meta
                    content="text/html; charset=UTF-8" http-equiv="Content-Type"><meta
                    content="/images/branding/googleg/1x/...
RawContent        : HTTP/1.1 200 OK
                    Content-Security-Policy-Report-Only: object-src 'none';base-uri 'self';script-src
                    'nonce-_zl_xlwMCRFpA5FRrQ3ZRA' 'strict-dynamic' 'report-sample' 'unsafe-eval' 'unsafe-inline'
                    https: ...
Forms             : {f}
Headers           : {[Content-Security-Policy-Report-Only, object-src 'none';base-uri 'self';script-src
                    'nonce-_zl_xlwMCRFpA5FRrQ3ZRA' 'strict-dynamic' 'report-sample' 'unsafe-eval' 'unsafe-inline'
                    https: http:;report-uri https://csp.withgoogle.com/csp/gws/other-hp], [Accept-CH,
                    Sec-CH-Prefers-Color-Scheme], [X-XSS-Protection, 0], [X-Frame-Options, SAMEORIGIN]...}
Images            : {@{innerHTML=; innerText=; outerHTML=<IMG id=hplogo style="PADDING-BOTTOM: 14px; PADDING-TOP: 
                    28px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px" alt=Google
                    src="/images/branding/googlelogo/1x/googlelogo_white_background_color_272x92dp.png" width=272
                    height=92>; outerText=; tagName=IMG; id=hplogo; style=PADDING-BOTTOM: 14px; PADDING-TOP: 28px;
                    PADDING-LEFT: 0px; PADDING-RIGHT: 0px; alt=Google;
                    src=/images/branding/googlelogo/1x/googlelogo_white_background_color_272x92dp.png; width=272;
                    height=92}}
InputFields       : {@{innerHTML=; innerText=; outerHTML=<INPUT type=hidden value=en-IN name=hl>; outerText=;
                    tagName=INPUT; type=hidden; value=en-IN; name=hl}, @{innerHTML=; innerText=; outerHTML=<INPUT
                    type=hidden value=hp name=source>; outerText=; tagName=INPUT; type=hidden; value=hp; name=source},
                    @{innerHTML=; innerText=; outerHTML=<INPUT type=hidden name=biw>; outerText=; tagName=INPUT;
                    type=hidden; name=biw}, @{innerHTML=; innerText=; outerHTML=<INPUT type=hidden name=bih>;
                    outerText=; tagName=INPUT; type=hidden; name=bih}...}
Links             : {@{innerHTML=<SPAN class=gbtb2></SPAN><SPAN class=gbts>Search</SPAN>; innerText=Search;
                    outerHTML=<A id=gb_1 class="gbzt gbz0l gbp1" href="https://www.google.co.in/webhp?tab=ww"><SPAN
                    class=gbtb2></SPAN><SPAN class=gbts>Search</SPAN></A>; outerText=Search; tagName=A; id=gb_1;
                    class=gbzt gbz0l gbp1; href=https://www.google.co.in/webhp?tab=ww}, @{innerHTML=<SPAN
                    class=gbtb2></SPAN><SPAN class=gbts>Images</SPAN>; innerText=Images; outerHTML=<A id=gb_2
                    class=gbzt href="https://www.google.com/imghp?hl=en&amp;tab=wi"><SPAN class=gbtb2></SPAN><SPAN
                    class=gbts>Images</SPAN></A>; outerText=Images; tagName=A; id=gb_2; class=gbzt;
                    href=https://www.google.com/imghp?hl=en&amp;tab=wi}, @{innerHTML=<SPAN class=gbtb2></SPAN><SPAN
                    class=gbts>Maps</SPAN>; innerText=Maps; outerHTML=<A id=gb_8 class=gbzt
                    href="https://maps.google.co.in/maps?hl=en&amp;tab=wl"><SPAN class=gbtb2></SPAN><SPAN
                    class=gbts>Maps</SPAN></A>; outerText=Maps; tagName=A; id=gb_8; class=gbzt;
                    href=https://maps.google.co.in/maps?hl=en&amp;tab=wl}, @{innerHTML=<SPAN class=gbtb2></SPAN><SPAN
                    class=gbts>Play</SPAN>; innerText=Play; outerHTML=<A id=gb_78 class=gbzt
                    href="https://play.google.com/?hl=en&amp;tab=w8"><SPAN class=gbtb2></SPAN><SPAN
                    class=gbts>Play</SPAN></A>; outerText=Play; tagName=A; id=gb_78; class=gbzt;
                    href=https://play.google.com/?hl=en&amp;tab=w8}...}
ParsedHtml        : mshtml.HTMLDocumentClass
RawContentLength  : 56651

This is the response that we get in return.

Understanding the response

Now, let’s understand what the response contains and what it means

Header - It states whether the request got fulfilled or not. Server returns the different status codes.

Body - Rest of the response contains the whole html. It contains all the tags of html like Images, Inputfields and Links.

Using cURL to talk to APIs

Just like you can request from any site, you can also request APIs to deliver data to you. This is not just for viewing the sites, but we can also interact with them.

Get Request - Get request means that the user will request some data from the server. Try this command in your command prompt and make sure to paste this in a single line.

curl.exe https://jsonplaceholder.typicode.com/posts/1

This command requests for the first post in this server and the output should be something like this.

{
  "userId": 1,
  "id": 1,
  "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
  "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
}

Post Request - Post request means the user will be sending data along with the request. Try this command in your command prompt and make sure to paste this in a single line.

curl.exe -X POST https://jsonplaceholder.typicode.com/posts 
-H "Content-Type: application/json" 
-d "{\"title\": \"My First Post\", \"body\": \"Hello World\", \"userId\": 1}"

Lets debug what the command is

-X states that I am sending the data

-H states what will the content type of the data in this case the application should expect JSON data

-d states that this is the data

The response should be

{
  "title": "My First Post",
  "body": "Hello World",
  "userId": 1,
  "id": 101
}

Common mistakes beginners make with cURL

There are some mistakes that beginners often make while using cURL. Knowing these mistakes prior helps to avoid them.

  • Missing https:// - cURL expects a full url not just the name.

  • Missing quotes - If your url has special characters like & and ? then you have to wrap the url within quotes. Ex. curl "https://api.com/search?q=apple".

  • Blindly pasting sensitive information in URL - Many tutorials blindly paste their api key in the URL, make sure it doesn’t happen.