DEV Community

Cover image for ๐Ÿ”ฅ Restful API Something You Might Not Know โšก
Truong Phung
Truong Phung

Posted on • Edited on

๐Ÿ”ฅ Restful API Something You Might Not Know โšก

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
Enter fullscreen mode Exit fullscreen mode

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?

1. 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.

2. 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.

3. 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.

4. 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).

5. 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.

If you found this helpful, let me know by leaving a ๐Ÿ‘ or a comment!, or if you think this post could help someone, feel free to share it! Thank you very much! ๐Ÿ˜ƒ

Top comments (0)