Skip to main content

Command Palette

Search for a command to run...

Does curl command really useful🤨?

Published
•4 min read•View as Markdown

Before giving answer to this question we should have idea about server. Because this command is related to server work.
so , Let’s start the blog with server definition:

Server : it is hardware or software that process the information over the network that take response from client (user) and return response. It shares file, content through protocol like HTTP so browser can access the webpage.

What is curl command and flow?

curl command is terminal command which pre-installed in majorly all OS (linux, maxos, window). It uses to transfer data between system and server mostly use this command for get web page content, API testing sending and receiving data over the internet through protocol like HTTP.

When user enter curl command with URL so how the curl establish the connection between terminal and server. curl does not handle the whole connection between server and terminal even curl is just a showcase for user behind the scene libcurl library establish the connection to server let’s go through step-by-step:

  1. Command Execution: user enter a command in terminal like curl https://chaicode.com and execute it.

  2. Search IP : After the execution libcurl help to get IP address of chaicode.com domain it asks to DNS resolver what is the IP of domain? DNS resolver start process to finding the IP of domain like Root server → TLD server → Authoritative nameserver and then get the IP of domain.

  3. Connection Establish: libcurl establish connection with domain IP usually TCP/IP connection it is 3-way handshake (SYN-SYN-ACK-ACK). check SSL certificate verify by trusted CA because the site is secure (https://chaicode.com).

  4. HTTP request: successfully establish the connection libcurl send the HTTP request same as like browser server get the request and return the response. response get by libcurl read it and curl print the response.

    Now It’s a complete flow of how behind the scene command execute, resolve IP, establish connection 3-way handshake between server , HTTP request , libcurl read the response and last curl print the response.

    If I make a roughly diagram of complete flow so it should be like this:

    curl (command execute) → libcurl (network library) → DNS resolver → Root server → TLD server → Authoritative nameserver (find IP) → HTTP request → server → HTTP response → libcurl → curl → Terminal Output.

Why programmers need curl?

Curl primarily uses to send network request API testing, file transfer, download files from command line supporting protocol like HTTP, HTTPS, FTP.

  • API testing and debugging : Curl uses for API testing and debugging with POST, GET, PUT, DELETE requests.

    POST : POST is used to send information to server

    GET : It retrieve data from server. It could be file, image, text.

    PUT : It uses to create or update a information from database.

    DELETE : It uses to delete resource from database.

  • There are some flags which help to show output of the curl command:

    1. -I : It uses to show the headers of the request URL in the output

      ex :- ‘curl --request GET 'https://api.nasa.gov/planetary/apod?api_key=<myapikey>&date=2020-01-01' -I’ this command show the header file of this URL.

    2. -v : It is a verbose command it shows the complete network connection request header, response header, DNS resolution, TLS, any many more.

      ex :- ‘curl --request GET 'https://api.nasa.gov/planetary/apod?api_key=<myapikey>&date=2020-01-01' -v’ this command show complete output with headers, request header, content, etc.

    3. -o : Stores the output in a file
      ex:- “curl --request GET 'https://api.nasa.gov/planetary/apod?api_key=$NASA_API_KEY&date=2020-01-01' --output curloutput“ this command save the output in curloutput file.

  • Server talk: curl directly get and post the data to server there is no need of browser to interact for testing API , check headers, even curl can show the DNS resolution, SSL certificate issue.

  • Browser vs Curl behaviour

BehaviourBrowserCurl
WebpageProvide webpage for usersOnly work on terminal
Headersend multiple headers (content-type, use-agent, accept-language, cookies)show only essentials headers (content-type, accept)
DNS supportit uses OS DNS and browser cache.It also use OS DNS but does not use browser DNS cache.
Redirect HandlingRedirect automatically flow even user don’t get itBy default it can not redirect.
CookiesIt stores cookies when user login or perform anything.It a stateless. cookies manually set.
JSBrowser supports js execution, DOM creation, API call via JS.If site load data from js so it shows incomplete or error.

Conclusion:

curl uses to send request over the network and with protocols like HTTP, HTTPS, FTP. it uses for downloading file and sending data. mostly use for API call and testing.

If you read this blog. I hope you found this helpful and get something interesting through this blog.