What is the difference between PUT, POST and PATCH? Ask Question

What is the difference between PUT, POST and PATCH? Ask Question

What is the difference between PUT, POST and PATCH methods in HTTP protocol?

ベストアンサー1

Difference between PUT, POST, GET, DELETE and PATCH in HTTP Verbs:

The most commonly used HTTP verbs POST, GET, PUT, DELETE are similar to CRUD (Create, Read, Update and Delete) operations in database. We specify these HTTP verbs in the capital case. So, the below is the comparison between them.

  1. Create - POST
  2. Read - GET
  3. Update - PUT
  4. Delete - DELETE

PATCH: Submits a partial modification to a resource. If you only need to update one field for the resource, you may want to use the PATCH method.

Note:
Since POST, PUT, DELETE modifies the content, the tests with Fiddler for the below url just mimicks the updations. It doesn't delete or modify actually. We can just see the status codes to check whether insertions, updations, deletions occur.

URL: http://jsonplaceholder.typicode.com/posts/

  1. GET:

GET is the simplest type of HTTP request method; the one that browsers use each time you click a link or type a URL into the address bar. It instructs the server to transmit the data identified by the URL to the client. Data should never be modified on the server side as a result of a GET request. In this sense, a GET request is read-only.

Checking with Fiddler or PostMan: We can use Fiddler for checking the response. Open Fiddler and select the Compose tab. Specify the verb and url as shown below and click Execute to check the response.

Verb: GET

url: http://jsonplaceholder.typicode.com/posts/

Response: You will get the response as:

"userId": 1,  "id": 1,  "title": "sunt aut...",  "body": "quia et suscipit..."

In the “happy” (or non-error) path, GET returns a representation in XML or JSON and an HTTP response code of 200 (OK). In an error case, it most often returns a 404 (NOT FOUND) or 400 (BAD REQUEST).

2) POST:

The POST verb is mostly utilized to create new resources. In particular, it's used to create subordinate resources. That is, subordinate to some other (e.g. parent) resource.

On successful creation, return HTTP status 201, returning a Location header with a link to the newly-created resource with the 201 HTTP status.

Checking with Fiddler or PostMan: We can use Fiddler for checking the response. Open Fiddler and select the Compose tab. Specify the verb and url as shown below and click Execute to check the response.

Verb: POST

url: http://jsonplaceholder.typicode.com/posts/

Request Body:

data: {
   title: 'foo',
   body: 'bar',
   userId: 1000,
   Id : 1000
}

Response: You would receive the response code as 201.

If we want to check the inserted record with Id = 1000 change the verb to Get and use the same url and click Execute.

As said earlier, the above url only allows reads (GET), we cannot read the updated data in real.

3) PUT:

PUT is most-often utilized for update capabilities, PUT-ing to a known resource URI with the request body containing the newly-updated representation of the original resource.

Checking with Fiddler or PostMan: We can use Fiddler for checking the response. Open Fiddler and select the Compose tab. Specify the verb and url as shown below and click Execute to check the response.

Verb: PUT

url: http://jsonplaceholder.typicode.com/posts/1

Request Body:

data: {
   title: 'foo',
   body: 'bar',
   userId: 1,
   Id : 1
}

応答:更新が成功すると、PUT からステータス 200 (本文にコンテンツが返されない場合は 204) が返されます。

4) 削除:

DELETE非常に簡単に理解できます。URIで識別されるリソースを削除するために使用されます。

削除が成功すると、レスポンス本文とともに HTTP ステータス 200 (OK) を返します。レスポンス本文には、削除されたアイテムの表現 (多くの場合、大量の帯域幅を必要とします) やラップされたレスポンス (以下の戻り値を参照) が含まれる場合があります。または、レスポンス本文なしで HTTP ステータス 204 (NO CONTENT) を返します。つまり、本文のない 204 ステータス、または JSEND スタイルのレスポンスと HTTP ステータス 200 が推奨されるレスポンスです。

Fiddler または PostMan で確認する:応答を確認するには Fiddler を使用できます。Fiddler を開き、[Compose] タブを選択します。以下に示すように動詞と URL を指定し、[Execute] をクリックして応答を確認します。

動詞:削除

URL: http://jsonplaceholder.typicode.com/posts/1

応答:削除が成功すると、応答本文とともに HTTP ステータス 200 (OK) が返されます。

PUTとPATCHの例

置く

名前を変更する必要がある場合は、PUT更新リクエストを送信します。

{ "first": "Nazmul", "last": "hasan" }

したがって、ここで名前を更新するには、データのすべてのパラメータを再度送信する必要があります。

パッチ:

パッチ リクエストでは、データの他の部分を変更したり影響を与えたりすることなく、変更する必要があるデータのみを送信することが指定されています。例: 名のみを更新する必要がある場合は、名のみを渡します。

詳細については、以下のリンクを参照してください。

おすすめ記事