mskw
mskw

Reputation: 10318

Is it good practice to send status data using Http Post?

I know post is usually used to tell the web server to store stuff, what is the right way to do if I just want to send some status data? Say, I'm posting a client status?

Upvotes: 2

Views: 60

Answers (3)

Gillsoft AB
Gillsoft AB

Reputation: 4225

It depends on your use case. In a strict rest environment thierry's answer is correct. However, when I use rest everyday towards financial services I must save all data without any changes according to law (e.g. traceability). In my case I cannot use strict rest so I use POST with the whole object with one or more changes to its attributes. The server responds with "201 Created" and a proper status message since every new request is a new resource.

Upvotes: 1

ernitingoel
ernitingoel

Reputation: 661

If you are not updating anything then POST is the method you should use it.

Upvotes: 1

Thierry Templier
Thierry Templier

Reputation: 202146

If you want to update the state of an existing resource, you must use the PUT method. But the latter corresponds to the update of the whole state, i.e. you need to send all fields. If you want to do a partial update, you should consider the PATCH one. You can send only the field or leverage the JSON Patch format.

Here are some samples:

  • Full update:

      PUT /someresource/someid
      Content-Type: application/json
      {
        (...)
        "status": "some status"
      }
    
  • partial update:

      PATCH /someresource/someid
      Content-Type: application/json
      {
        "status": "some status"
      }
    
      204 No content
    
  • partial update with JSON Patch:

      PATCH /someresource/someid
      Content-Type: application/json
      [
        {
          "op": "replace",
          "path": "/status",
          "value": "some status"
        }
      ]
    
      204 No content
    

If "send some status data" corresponds to add something in your datastore, you should consider to use a POST method but on the list resource (single resource is used for PUT and PATCH for a specific resource element). Here is a sample:

    PUT /someresource/
    Content-Type: application/json
    {
      (...)
      "status": "some status"
    }

    204 No content
    Location: http://.../someresource/newid

Here are some additional links that could help you:

Hope it will help you, Thierry

Upvotes: 1

Related Questions