本文介绍了发送命令的 RESTful 方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何向 RESTful 服务器发送命令"?

How do you send "commands" to a RESTful server?

用例:我的服务器缓存某些信息,这样它就不必在每次请求信息时都读取数据库.我需要一种方法来从我的客户端应用程序发送命令来告诉服务器刷新缓存.您会在诸如.../flush_cache"之类的 URL 上使用 POSTPUT 吗?

Use case: My server caches certain information so that it does not have to read the database every time that information is requested. I need a way to send a command from my client application to tell the server to flush the cache. Would you use POST or PUT on some URL like ".../flush_cache"?

command"并不是真正需要Representational State Transfer"的数据,除非被转移的状态是命令的结果——开关关闭"、缓存被刷新"等.作为一般规则,REST如何向服务器发送命令?

The "command" is not really data that needs "Representational State Transfer", unless the state being transferred is the result of the command -- "switch is off", "cache is flushed", etc. As a general rule, how does REST send commands to the server?

推荐答案

我在过去的项目中经常遇到这样的情况.由于 REST 很好......关于资源,如何处理真正属于 RPC 性质的事情并不总是很清楚.

I have come across such a situation a lot in a past project. Since REST is well...about resource, it is not always clear how to deal with things that are Really RPC in nature.

解决这个问题的一个简单方法是将其视为休息的官僚部分,请求本身可以是一种资源:

An easy way to get around this is to think of it as the bureaucratic part of rest, the request can be a resource itself :

1.你想在我的服务器上触发一个命令?先填写这个表格 I90292 并发送给我们":

1 . "You want to trigger a command on my server ? first fill this form I90292 and send it to us" :

POST /bureaucracy/command-request
Content-Type: application/x-www-form-urlencoded
Content-Length: ...
  1. 好的,我们会看看我们能做些什么.你的案件编号是 999"

  1. "Ok we will see what we can do. your case number is 999"

201 Created(或 202 根据 Kugel 的评论被接受)位置/bureaucracy/command-request/999

201 Created (or 202 Accepted as per Kugel's comment)Location /bureaucracy/command-request/999

然后客户定期检查

GET/bureaucracy/command-request/999

GET /bureaucracy/command-request/999

希望他得到如下回复

200 OK
<command-request>
  <number>999</number>
  ...
  <result>Success</result>
</command-request>

当然,如果官僚服务有很好的客户服务,它会提供客户在完成后给他打电话,如果他愿意:
您想在我们的服务器上触发命令?请填写此表格并发送给我们.请注意,您可以加入您的联系信息,以便我们在完成后给您打电话"

Of course if the bureaucratic service has great customer care it would offer the customer to call him when it is done if he wants :
"You want to trigger a command on our server? Kindly fill this form and send it to us. Note that you can join your contact info so we can call you when it is done"

POST /bureaucracy/command-request?callback=client.com/bureaucracy/inbox

或者作为自定义http头X-Callback: http://client.com/bureaucracy/inbox

Or as a custom http header X-Callback: http://client.com/bureaucracy/inbox

这篇关于发送命令的 RESTful 方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 08:39