How do you implement this in a rest service?
I want to create开发者_运维百科 a restful service that acknowledges a previous request and gets the next message in a single call. This saves on unnecessary calls to the server. Is it acceptable to perform two operations in a single rest call? If so what rest verb would you use as it seems like a combination of get and put?
Excuse my ignorance as this is new to me.
Here is one way to transfer state to a server, and in the response acknowledge the receipt of the content, whilst returning an additional response. There does not exist any mechanism in the protocol to do this generically, but you can create resources that perform these combined operations.
GET /Step1
=>
200 OK
Content-Type: application/vnd.hal+xml
<resource rel="self" href="http://example.org/step1">
<title>Hello User</title>
<link rel="next" href="http://example.org/step2" />
<resource>
POST http://example.org/step2
<!-- Insert Content here -->
=>
200 OK
Content-Type: application/vnd.hal+xml
<resource rel="self" href="http://example.org/step2">
<title>Thank you for your content</title>
<response>This is your answer</response>
<link rel="next" href="http://example.org/step3" />
<resource>
I'm using hal as media type just as an example because it has a well defined way of including links. Also, I am abusing the standard "next" link relation as you normally use a GET with next. You would probably need a custom link relation to really do what I am suggesting.
This is how I've been approaching this problem.
REQUEST
PUT /resource # (or POST)
Accept: application/xml
Content-Type: application/xml
<Resource>
<Blah/>
</Resource>
RESPONSE
HTTP 1.1 200 OK # (or 201 Created)
Content-Type: application/xml
Content-Location: /resource
<Resource>
<Blah/>
</Resource>
I could be wrong on the use of Content-Location in the response (HTTP 1.1 spec says its use is undefined for PUT and POST), but that's what I've been using. I'd be interested to know how others are doing this.
In general, I don't think there's a problem with returning content with a POST or a PUT.
John
精彩评论