Logo

dev-resources.site

for different kinds of informations.

Understand cURL more in Just 10 Minutes!🔥🔥

Published at
12/26/2024
Categories
api
documentation
webdev
coding
Author
fallon_jimmy
Categories
4 categories in total
api
open
documentation
open
webdev
open
coding
open
Author
12 person written this
fallon_jimmy
open
Understand cURL more in Just 10 Minutes!🔥🔥

Almost every API shows how to interact with the API using cURL. So before moving on, let’s pause a bit and learn more about cURL.

  • Why cURL?
  • Try using curl to GET a web page
  • Requests and responses include headers too
  • Query strings and parameters
  • Common curl commands related to REST
  • Example curl command curl
  • Friendly tool recommendation

Why curl?

One of the advantages of REST APIs is that you can use almost any programming language to call the endpoint. The endpoint is simply a resource located on a web server at a specific path.

Each programming language has a different way of making web calls. Rather than exhausting your energies trying to show how to make web calls in Java, Python, C++, JavaScript, Ruby, and so on, you can just show the call using curl.

curl provides a generic, language-agnostic way to demonstrate HTTP requests and responses. Users can see the format of the request, including any headers and other parameters. Your users can translate this into the specific format for the language they’re using.

Try using curl to GET a web page

As mentioned earlier, one reason REST APIs are so familiar is that REST follows the same model as the web. When you type an http address into a browser address bar, you’re telling the browser to make an HTTP request to a resource on a server. The server returns a response, and your browser converts the response to a more visual display. But you can also see the raw code.

To see an example of how curl retrieves a web resource, open a terminal and type the following:

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

Curl will retrieve the HTML code for the site example.com. The browser’s job is to make that code visually readable. curl shows you what you’re actually retrieving.

Requests and responses include headers too

When you type an address into a website, you see only the body of the response. But actually, there’s more going on behind the scenes. When you make the request, you’re sending a request header that contains information about the request. The response also contains a response header.

  1. To see the response header in a curl request, include -i in the curl request:
curl http://example.com -I
Enter fullscreen mode Exit fullscreen mode

The header will be included above the body in the response:

~/projects $ curl http://example.com -I
HTTP/1.1 200 OK
Content-Encoding: gzip
Accept-Ranges: bytes
Cache-Control: max-age=604800
Content-Type: text/html; charset=UTF-8
Date: Tue, 04 Dec 2018 04:35:43 GMT
Etag: "1541025663+gzip"
Expires: Tue, 11 Dec 2018 04:35:43 GMT
Last-Modified: Fri, 09 Aug 2013 23:54:35 GMT
Server: ECS (sjc/4F91)
X-Cache: HIT
Content-Length: 606
Enter fullscreen mode Exit fullscreen mode
  1. To limit the response to just the header, use -I:
curl http://example.com -I
Enter fullscreen mode Exit fullscreen mode

The header contains the metadata about the response. All of this information is transferred to the browser when you make a request to a URL in your browser (for example, when you surf to a web page online), but the browser doesn’t show you this information. You can see the header information using the Chrome Developer Tools console by looking on the Network tab.

  1. Now let’s specify the method. The GET method (read) is implied by default when no other method is specified, but we’ll make it explicit here with the -X parameter:
curl -X GET http://example.com -I
Enter fullscreen mode Exit fullscreen mode

When you go to a website, you submit the request using the GET HTTP method. There are other HTTP methods you can use when interacting with REST APIs. Here are the common methods used when working with REST endpoints:

Image description

Note:GET is used by default with curl requests. If you use curl to make HTTP requests other than GET, you need to specify the HTTP method.

Query strings and parameters

The zip code (zip), app ID (appid), and units (units) parameters were passed to the endpoint using “query strings.” The ? appended to the URL indicates the start of the query string. The query string parameters are the parameters that appear after the ?:

?zip=95050&appid=APIKEY&units=imperial
Enter fullscreen mode Exit fullscreen mode

(In the above code, replace APIKEY with your actual API key.)

After the query string, each parameter is separated from other parameters by the ampersand & symbol. The order of the query string parameters doesn’t matter. The order only matters if the parameters are on the left of the query string (and thus part of the URL itself). Any configurable parts of the endpoint that appear before the query string are called path parameters (we’ll dive into these later).

Common curl commands related to REST

curl has a lot of possible commands, but the following are the most common when working with REST APIs.

Image description

Example curl command

Here’s an example curl request that combines some of these commands:

curl -i -H "Accept: application/json" -X POST -d "{status:MIA}" http://personsreport.com/status/person123
Enter fullscreen mode Exit fullscreen mode

The request could also be formatted with line breaks to make it more readable:

curl -i \
     -H "Accept: application/json" \
     -X POST \
     -d "{status:MIA}" \
     http://personsreport.com/status/person123 \
Enter fullscreen mode Exit fullscreen mode

(Line breaks are problematic on Windows, so I don’t recommend formatting curl requests like this.)

The Accept header tells the server that the only format we will accept in the response is JSON.

Friendly tool recommendation

Apidog streamlines API processes with testing, debugging, design, mocking, and documentation tools, offering an intuitive interface for easy API requests.

1.Sign up or download:

2.Import cURL Commands: Access the Apidog import window. Paste cURL commands into the text box for seamless integration.

cURL

cURL

3.Configure Request parameters: Navigate to the API body. Set the frequency of API headers, body, and other settings.

cURL

4.Test and Refine

cURL

Apidog is user-friendly, cURL provides richer functionality.

documentation Article's
30 articles in total
Favicon
LaTeX for Beginners in 6 Minutes
Favicon
Using Direct Line botframework in a React Native Application to connect to Copilot Studio Agent
Favicon
The Importance of Writing Meaningful Code and Documentation
Favicon
Versioning in Go Huma
Favicon
Documentation Release Notes - December 2024
Favicon
What Content to Create and How to Publish It, Part 1
Favicon
Simplify Your Billing Process with an Invoice Template Google Docs
Favicon
Mastering Developer Documentation: A Journey Beyond the Basics
Favicon
Maintainability Is All You Need
Favicon
API Documentation: How to Write It, Template and Examples
Favicon
How to Write a Troubleshooting Guide That Actually Helps Users
Favicon
The Importance of Reading Documentation: A Lesson from Nvidia Drivers
Favicon
Unpacking Technical Debt: The Types Every Dev Should Know
Favicon
Reasons Why Developers Hate Your Docs
Favicon
Data Analytics Skills for Technical Writers
Favicon
How We Do Documentation Engineering
Favicon
[Boost]
Favicon
Things are moving fast! Check out Reldens new docs here: https://lnkd.in/dFjgAfge. It’s really worth it!
Favicon
JSDoc: La Guía Definitiva para Documentar tu Código JavaScript
Favicon
Dominando el Testing y la Documentación en React: Una Guía Completa
Favicon
AI in Healthcare Documentation: The Future of Medical Records
Favicon
Understanding User Needs in Technical Writing: How Frameworks Like Diátaxis Help
Favicon
Documentation Release Notes - November 2024
Favicon
How to view API request examples in a ReadMe documentation.
Favicon
Supercharge Your Project Documentation: Introducing project-readme-gen – An AI-Powered README Generator
Favicon
Invoice & Reimbursement Document Processing
Favicon
Understand cURL more in Just 10 Minutes!🔥🔥
Favicon
Moderniser son Dossier d'Architecture Technique : Guide pratique pour 2024
Favicon
The True Value of Your Product Lies in Its Documentation
Favicon
Integrating OpenAPI Documentation and Swagger UI in Spring Boot

Featured ones: