- vừa được xem lúc

RESTful API, Something You Might Not Know

0 0 3

Người đăng: Truong Phung

Theo Viblo Asia

Have you ever wondered why we use POST method for creating resouces and GET for retrieving Data but not the other way around. Let's have a look at some considerations.

About Query Parameters

Can we put query params to POST method?

Yes, you can include query parameters in a POST request. While query parameters are more commonly associated with GET requests, they can also be used with POST requests to send additional data to the server.

Here's how it works:

Sending Query Parameters: You can append query parameters to the URL when making a POST request. For example:

POST /api/resource?param1=value1&param2=value2

Use Cases: Query parameters can be used for filtering, sorting, or specifying optional behavior in a POST request, complementing the data sent in the request body.

In summary, while it's less conventional, using query parameters in a POST request is valid and can be useful in certain scenarios.

About Body Data

Can we send a body data with GET method?

No, you generally should not send body data with a GET method. GET requests are designed to retrieve data and should be idempotent and cacheable. The body in a GET request is usually ignored by servers and not supported by most HTTP clients and servers. Instead, use query parameters for sending data with a GET, or switch to a POST if you need to include a body.

About Returning Data

Can we get data with POST method?

Yes, you can retrieve data with a POST method, though it is not the conventional use. POST is typically used for creating or updating resources, but it can be used to fetch data when you need to include complex body data in the request. However, GET is more suitable for data retrieval as it is designed to be idempotent and cacheable.

Differences between POST and GET method?

Purpose:

GET: Primarily used to retrieve data from the server. Query parameters in GET requests are often used to filter or sort the data being requested.

POST: Primarily used to send data to the server, typically for creating or updating resources. Query parameters in POST requests can provide additional context or options for the operation being performed.

Visibility:

GET: Query parameters are included in the URL and are visible in the browser’s address bar, making them less secure for sensitive data.

POST: Query parameters can be included in the URL, but the main data is sent in the body of the request, which is not visible in the address bar.

Data Length:

GET: There are limitations on the length of the URL, which can restrict the amount of data you can send via query parameters (commonly around 2000 characters).

POST: Generally allows larger amounts of data to be sent in the request body without strict limits like those imposed on URL length.

Caching:

GET: Responses to GET requests can be cached by browsers and intermediate proxies, improving performance for repeated requests.

POST: Responses to POST requests are generally not cached, as they often represent actions that change server state (like creating a resource).

Idempotency:

GET: GET requests are idempotent, meaning making the same request multiple times will yield the same result without side effects.

POST: POST requests are not necessarily idempotent; sending the same request multiple times may result in multiple resource creations or updates.

Summary:

Use GET for retrieving data, typically with filtering or sorting via query parameters, and when data security is less of a concern.

Use POST for sending data that may alter server state, and when you need to send larger or more complex data payloads securely. Query parameters can still provide additional options in POST requests, but the main data usually goes in the request body.

How about PUT, DELETE method?

You can technically retrieve data with PUT and DELETE methods as well, but it's not their intended use:

PUT: Primarily used for updating a resource at a specified URI. While you can include a response body in a PUT request, the intent is generally to send data to be updated rather than retrieve data.

DELETE: Designed to remove a resource. Although a server can return a response body when deleting (like a confirmation message or deleted resource details), DELETE is not meant for data retrieval.

In general, using GET for retrieving data aligns with HTTP standards, while POST, PUT, and DELETE are used for creating, updating, and deleting resources, respectively.

Bình luận

Bài viết tương tự

- vừa được xem lúc

Flutter - GetX - Using GetConnect to handle API request (Part 4)

Giới thiệu. Xin chào các bạn, lại là mình với series về GetX và Flutter.

0 0 351

- vừa được xem lúc

API vs WebSockets vs WebHooks: What to Choose?

. Khi xây dựng bất kì một ứng dụng nào, chúng ta đều cần phải có một cơ chế đáng tin cậy để giao tiếp giữa các thành phần của nó. Đây là khi APIs, WebSockets và WebHooks được ứng dụng vào.

0 0 101

- vừa được xem lúc

Sử dụng Fast JSON API serialization trong Ruby on Rails

Ở bài viết này chúng ta sẽ thử tạo 1 project API sử dụng gem fast_jsonapi cho serializer. Đầu tiên là tạo một project API mới. $ rails new rails-jsonapi --database=postgresql --skip-action-mailbox --skip-action-text --skip-spring -T --skip-turbolinks --api. .

0 0 131

- vừa được xem lúc

Test thử ba loại API chụp màn hình Windows

Hiện tại, Windows cung cấp khoảng ba cách để chụp màn hình. Thế thì cái nào là nhanh nhất? Tôi muốn test thử từng cái.

0 0 71

- vừa được xem lúc

Ngừng sử dụng REST cho API — GraphQL là cách tốt hơn

Mở đầu. REST đã được nhiều developers sử dụng để gửi dữ liệu qua HTTP trong khi GraphQL thường được trình bày như một công nghệ thay thế các API REST.

0 0 98

- vừa được xem lúc

Quản lý và sử dụng API trong Nuxt bằng cách sử dụng Repository Pattern

Mở đầu năm mới, à nhầm, mở đầu bài viết. Cái tên NuxtJS chắc hẳn cũng không còn xa lạ gì với những bạn yêu thích VueJS nữa, đương nhiên mình cũng là một chàng trai dành tình yêu to lớn cho frameworks này.

0 0 226