DEV Community

Cover image for Getting Started with cURL
Anoop Rajoriya
Anoop Rajoriya

Posted on

Getting Started with cURL

Content List

Introduction

cURL stand for Client URL its a command line tool or a library used to transfer url data over internet through the teminal. If a browser is a high level tool to make use to view code than a curl is a low level tool for the tech peoples to make request on server and see respone on terminal.

Its comes with support of vast application layer like http, https, ftp, sftp, scp, and smtp protocols. and compatible with features like:

  • Method Support: its support varity of http methods like GET, POST, etc.
  • Header Manipulation: allow use to view and manipulate request headers.
  • Authentication: it handle basic auth, oauth, and bearer tokens.
  • File Transfer: allow to download and sent files on server.

cURL provide a no-nonsence way to make request to servers. It provide a fast, reliable and scriptable toolkit for saving time and resources. it provide following compatibilities:

  1. Api Testing & Debugging: programmers curl to test servers with diffrent test cases and view internal flow of header and data payloade if in case of failure.
  2. Authomation & scripting: because its command line tool it can easily embedded into scripts make auto network request. Programmers use it to install current latest packages during load.
  3. Language Agnostic Documentation: most of the servers like strip and aws has a curl implementation in those docs because it like a universal language which define a shape of the network requests.
  4. User-Agent Simulation: curl help programmers to test server response for different devices type it allow to simulate request for any device.

Making Request Using cURL

cURL comes with almost all modern operating systems as a native cli tools by using terminal you can make use cURL. Lets make a request to google.com server.

url https://www.google.com
Enter fullscreen mode Exit fullscreen mode

When you run the above command on the terminal you got a lines of gibrish on the terminal its a html code return by the google.com server. By default cURL makes the GET request which means it ask for getting content files. The content file type is defined in header to see use the -I flag.

url -I https://www.google.com
Enter fullscreen mode Exit fullscreen mode

This allow you to fetch only response header. After running command you get response like that:

i'm_Anoop\Desktop\blogs_doc>curl -I https://www.google.com
HTTP/1.1 200 OK
Content-Type: text/html; charset=ISO-8859-1
...
Enter fullscreen mode Exit fullscreen mode

Within the header you get lot of information by the server side. Here you see 200 OK meaning server successfully find your requested content and Content-Type: text/html; tell you file type you get is html code. In browser it looks google home page but in terminal it looks like a gibrish text. cURL provide a -o flag which enable you to take output content witin any file.

curl -o index.html https://www.google.com
Enter fullscreen mode Exit fullscreen mode

This command generate a index.html file in current directory with the response content return by the url.

cURL Request & Response

When you run any cURL command in terminal like curl https://www.google.com it wrap all information you provided like url, header or data if provided it wrap all these thing into a single package called request and when server ready to resend data your requested it also wrap all thing into a single package called response. both request and response containing many information but the major three is:

Request Package:

  • Methods: its define type of request tell to server what to do. In curl -X flag used to define type, its mostly used post, put request. Default type is GET where it -X flag not used.
  • URL: its a unique resources locator used to sent request on server.
  • Headers: its information section of the request which provide context, metadata about the request like Content-Type, User-Agent.

Response Package:

  • Status: its containing 3-4 digits number indicating the status of the request process at server side.
  • Headers: at the server side which provide metadata about the respone.
  • body: this is the section of response which contain the actual data or files return by the server.

cURL provide a verbose mode to see how these request and response looks in real. To use it you want ot add the -v flag which list all information of requst and response happens.

curl -v https://www.google.com
Enter fullscreen mode Exit fullscreen mode

This command list all information with the two symbols. > this symbol indicates that lines with that is a part of request made by your side and < symbol indicates like start with that part of response sent by the server.

Using cURL To Talk APIs

In client-server architectrue APIs are the interface used to exchange infomation. This exchage use pre-defined process in different request calls like getting some data from server use GET Request, POST Request use to post data on, PUT and DELETE used to update and delete some data on server.

cURL provide flags like -X, -d, and -H for configures these methods, data and header information to make the intended request on server. Let's see the how it work:

1. Get Request

Making get request not method because by default cURL aleady make get request only need to add url and headers for content type and auth token if needed. The given command return a post json object.

curl https://jsonplaceholder.typicode.com/posts/1 -H "Content-Type:text/json"
Enter fullscreen mode Exit fullscreen mode

2. POST Request

Post request used to post some data like user sign, login, and other info on server. In that you need to explicitly tell curl that you want ot make a post request to given url. You can use header to define data type and tokens about the request or user.

curl -X POST https://jsonplaceholder.typicode.com/posts -H "Content-Type: application/json" -d "{\"title\": \"cURl\", \"body\": \"beginner\", \"userId\": 1}"
Enter fullscreen mode Exit fullscreen mode

This return stored post object in server, indicating that is request successfully resolved by the server. In the above command -X used to define request type here is POST because we want to post data, -H is used to add type of data you sent to server here is json so i added "Content-Type: application/josn", and -d used for payloade want to post. For PUT, DELETE and ther are lot of methods can use with intended header and payloads.

Common Mistakes Beginners Make With cURL

  • Special Characters: Special characters are the big problem chaos in terminals because different terminal support different types of characters. Some times url contains special characters which cause probelm in command to avoid always follow terminal character support criteria and wrap url into qouts.
  • Headers: In some server restrict the content type, force user to only give data in supported type, most server use json for data exchange So always specify content type. Token like Bearer-token, cookie are also requerd some cases so double check it also.
  • URL Redirect: In some cases server redirect to anoter url but cURL stop when happen ensure for that type use -L flag to track redirecting urls.
  • Output flags: lots of beginners are confused -o and -O these are used for different usecases -o used to store response data in file which name is specified in command but the -O flag tell cURl to use the file name from the url.

Top comments (0)