它是确定的REST API,以通过两种HTTP方法被曝光? [英] Is it ok for a REST api to be exposed via two HTTP methods?

查看:140
本文介绍了它是确定的REST API,以通过两种HTTP方法被曝光?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是,我们有一个搜索API的复杂的查询字符串,并希望让用户有使​​用身体,而不是方便。因此,我们要允许GET和POST(或PUT)。

The problem is that we have a complex query string for a search api and want to let the users have convenience of using body instead. So we want to allow both GET and POST(or PUT).

据我所知,有必应搜索是一个只读操作的辩论,它应该得到的只是按REST标准。也把不缓存友好的,因为我明白了。
但我也知道,它的确定,有时从REST的标准偏差。但是否有意义有两种方式供客户方便?

I understand that there will be a debate of search being a read only operation and it should be GET only as per REST standards. Also PUT is not cache friendly as i understand. But i also know that its ok to deviate at times from the REST standards. But does it make sense to have two methods for client's convenience?

推荐答案

使用 POST 直接查询数据的的好事,precisely对于您提到的原因。如果你的搜索字符串是复杂的,也许你可以通过把查询过程分为两个步骤简化事情 - 一个涉及 POST ,另一个直涉及 GET 秒。

Using POST directly to query data is not a good thing, precisely for the reasons that you mentioned. If your search string is complex, perhaps you could simplify things by splitting the querying process in two steps - one involving a POST, and another one involving straight GETs.

第一步是创建使用 POST 查询模板。查询字符串是通过邮件正文发送,并成为一个新的资源,用户可以通过 GET 查询。查询字符串允许参数,类似于SQL查询的方式。以在查询会是什么样胡乱猜测,这里是一个例子:

The first step creates a query template using the POST. The query string is sent via message body, and becomes a new resource that users can query through GET. Query string allows for parameters, in a way similar to SQL queries. Taking a wild guess at how your query might look, here is an example:

(userName = $name) || (createdBefore > $asOf && deleted=false)

您的用户会 POST 在一个消息体,并获得了新的资源标识符回来。该资源标识参数化的查看到您的数据。比方说,对这一观点的资源ID为 aabb02kjh 。现在,您的用户可以查询它是这样的:

Your users would POST this in a message body, and get a new resource identifier back. This resource identifies a parameterized "view" into your data. Let's say the resource id for this view is aabb02kjh. Now your users can query it like this:

https://app.yourserver.net/aabb02kjh?name=airboss&asof=20140101

这增加了一些复杂的API,但是它允许用户定义和重用查询模板非常简单和标准的查询字符串。

This adds some complexity to your API, but it lets users define and reuse query templates with very simple and standard query strings.

这篇关于它是确定的REST API,以通过两种HTTP方法被曝光?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆