Using LINK and UNLINK HTTP verbs in a REST API

前端 未结 1 926
伪装坚强ぢ
伪装坚强ぢ 2021-02-02 09:55

I am currently working on implementing a REST API. I have a resource model with a large number of relationships between the individual resources.

My question is: how do

1条回答
  •  攒了一身酷
    2021-02-02 10:28

    I use LINK and UNLINK in my (bespoke, company-internal) web app. I use http://tools.ietf.org/html/draft-snell-link-method as my implementation reference.

    I found that there are three types of clients:

    1. Those that only support GET and POST, taking their cues from HTML's
      element.
    2. Those that only support GET, PUT, POST and DELETE. These take their cues from CRUD and RPC-pretending-to-be-REST type APIs.
    3. Those that allow any method. The publication of PATCH as an official RFC has increased the amount of these, as has the growth of WebDAV, although sometimes category 2 clients support PATCH too.

    Since we currently develop our clients in-house we don't have this problem, but I have looked into it and did consider the pros and cons before defining my API, in case we did want to allow third-party clients. My solution (since we needed to support HTML clients without Javascript anyway) was to allow POST to override the method by supplying a _METHOD field (application/x-www-form-urlencoded) and then have my post() function on the back-end palm off to the appropriate function for the intended HTTP method. That way, any client in the future that is in, say, class 2 above, can use PUT and DELETE but wrap PATCH, LINK and UNLINK in a POST request. You get the best of both worlds: rich methods from clients that support it, and yet still support low-quality clients through POST-tunnelling.

    0 讨论(0)
提交回复
热议问题