How is a HTTP PUT request typically issued?

后端 未结 5 1953
天涯浪人
天涯浪人 2021-02-05 06:31

I know HTTP PUT is an idempotent request that store something at a specific URI, according to the definition (quoted from the rfc)

The PUT method requests that t         


        
5条回答
  •  猫巷女王i
    2021-02-05 07:16

    So a HTTP PUT request is often issued to replace the currently stored resource at a given URI. For example, there's a book stored at https://example.org/book/1 where the data can be representated in JSON as follows,

    $ curl --request GET https://example.org/book/1
    {
        "title": "Stackoverflow Compilation Book 1",
        "year": 2019
    }
    

    Suppose someone wants to fix the year field because the fictional book was published last year (2018), he/she would have to send the COMPLETE updated book info over through a HTTP PUT request.

    $ curl --request PUT
          --header "Content-Type: application/json"
          --data '{"title": "Stackoverflow Compilation Book 1", "year": 2018}'
    

    Notice the change in year attribute.

    Considering a HTTP PUT request is essentially a replace operation, one can also replace the book represented by the URI to something else. For instance,

    $ curl --request PUT
          --header "Content-Type: application/json"
          --data '{"title": "Some random book that nobody publishes", "year": 2019}'
    

    The attached data can be in any format (usually also specified in the request header Content-Type, as shown above), as long as it is supported, usually reported by Accept response header (which denotes what kind of data type the application is willing to deal with). Further validation would be handled by the application code to determine whether the submitted data is valid.

提交回复
热议问题