HTTP Methods, CRUD Operations, and cURL

HTTP Methods, CRUD Operations, and cURL

According to the MDN Docs, there are 9 request methods defined by HTTP (HyperText Transfer Protocol). When we're developing APIs for our application, we generally use only 4 of them

Before we dive into what each of these methods are and what they do, let's understand a bit about databases and the operations performed on them

Database

Database allows us to store the data for our application permanently, efficiently, and allows for concurrent access by a large number of users. There are mainly two types of databases

  • SQL Database

This stores the data in the form of tables. It's used mainly when we need to define complex relations in the data, which can be defined using foreign keys from one table to another. Another use case is when we might need to modify multiple a large number of rows in a single query/transaction

In summary, SQL can be used to store a large amount of data in a well structured format using tables

  • NoSQL Database

Here, data is stored in the form of documents, which consist of multiple key-value pairs, just like a JSON. It's used mainly when we don't have a defined structure required to store our data. Also when there's not many relations in our data. Ease of development is another factor, especially when you're using a JavaScript framework like Node.js / Express.js

CRUD Operations

When interacting with any type of databases, we mainly perform the following operations

  1. Create - to create a new entry / entries in the database
  2. Read - to read an entry / entries from the database
  3. Update - to modify an existing entry / entries in the database
  4. Delete - to delete an entry / entries from the database

These operations are abbreviated as 'CRUD' operations, and are usually the only operations we'll build our API to perform.

Using cURL

cURL is a command line tool we can use to directly send requests to (and receive corresponding responses from) a server. We'll be using it to send HTTP requests to the backend.

We will understand the main parts of an HTTP request we'll be using while developing APIs, and how to use these parts while using cURL.

URL

This is the address where the backend is running

https://localhost:3000        

It consists of the protocol (https://), the IP address/domain name (localhost), and the port number (:3000)

We can specify the URL in cURL using either of the following

curl https://localhost:3000
curl --location https://localhost:3000        

(The --location flag follows redirects)

Method

Well, now's the right time to understand the HTTP methods we will be using


The 4 HTTP Methods

Corresponding to the CRUD operations on the database, these methods are used

  • POST

POST method is used to perform Create operations at the backend. When sending a POST request, we send data in the body of the request which tells the backend what data to insert

  • GET

GET method is used to perform Read operations. A query parameter or a URL parameter may be used to specify what data to read (both of these parameters will be understood in the next sections)

  • PUT

PUT method is used to perform Update operations. A parameter is used to specify what data to update, and the body of the request defines what the data should be updated to

  • DELETE

DELETE method is used to perform Delete operations. A parameter is used to specify what data to delete


In cURL, we can use the -X flag to specify the method we need to use

curl --location https://localhost:3000 -X POST
curl --location https://localhost:3000 -X GET
curl --location https://localhost:3000 -X PUT
curl --location https://localhost:3000 -X DELETE        

(If no method is specified, cURL defaults to the GET method)

Body

For the POST and PUT requests, we need to send some data. This is sent in the body. We mostly use either FormData or JSON

  • FormData can be easily added to the body in the form of key-value pairs, where both the key and the value are in the form of a string

A pair is separated from another using the character '&'. For example:-

first_name=Yash&last_name=Jaiswal        

To send it as body in curl, we can use the -d flag

curl --location https://localhost:3000 -X POST -d "first_name=Yash&last_name=Jaiswal"        

  • JSON object is another data format with key-value pairs, but the key and value need not be a string strictly. These key-value pairs are enclosed in curly braces {} just like an object in JavaScript

To add a JSON object to the body of a request, we'll need to also set the header of our request in order to tell the server that the body of this request is of the json format

We can set such headers in curl using the -H flag. We'll be setting the "Content-Type" header to "application/json"

Let our JSON object be

{
    "first_name": "Yash",
    "last_name": "Jaiswal"
}        

To send this JSON object in the body, we'll construct the curl command as follows

curl --location https://localhost:3000 -X POST -H "Content-Type: application/json" -d '{"first_name": "Yash", "last_name": "Jaiswal"}'        

(The Content-Type for FormData is application/x-www-form-urlencoded, in case you need to set it manually)

Parameters

Parameters are added into the URL itself, and processed at the backend. There's two types of parameters we'll be dealing with, while developing APIs.

  • URL Parameters

These parameters are added just after a slash (/), the logic for handling this parameter is added in the backend

For example, while editing this article, I see the following address in my browser's address bar

https://www.dhirubhai.net/article/edit/7239502071559782401        

We can safely assume that the number after 'edit/' is the id for the article, and not the actual path that the server is serving

While handling this URL in the backend, the pattern would be matched something like the following, to extract the articleID

https://www.dhirubhai.net/article/edit/:articleID        

  • Query Parameters

These parameters exist in key-value format, where both the key and value are strings.

Using query parameters is the best choice if you need to perform pagination for the data you're fetching.

The parameters are added to the URL after a question mark (?) and separated by an ampersand (&). A parameter is represented as key=value

Let's say we need to pass two parameters, firstly page = 1, and the limit = 10 (number of items to display on a page), to the following URL

https://localhost:3000/posts        

We'll modify the URL as follows

https://localhost:3000/posts?page=1&limit=10        

In cURL, you would simply change the URL in the request accordingly

Final Words

There is obviously much more to learn about HTTP requests and database operations, but I've tried to cover the basic ones every developer needs to know.

cURL is an amazing tool to quickly test a few endpoints in your API, but as your project grows you'll need to use other tools. An excellent example would be Postman, which is widely used by developers to test, maintain, and generate documentation for, their APIs. When you implement security in your webapp, you'll need to deal with JWT tokens, cookies, more headers, etc. so tools like Postman make it easy for developers to keep track of all of it.

Thank you for reading. Please share your thoughts in the comments.

要查看或添加评论,请登录

Yash Jaiswal的更多文章

  • Building a Basic Server with Golang

    Building a Basic Server with Golang

    Today, let's build a basic server with Go, using only the built-in modules. First let's understand what a server is.

  • The Beginning

    The Beginning

    Hello, everyone To help myself and you, the readers, be a better developer, and to encourage everyone's learning…

    1 条评论

社区洞察

其他会员也浏览了