drx3157 2012-08-27 00:51
浏览 38
已采纳

可以在RESTful中传递等号吗?

My RESTful interface works fine until I try to pass a WHERE statement

Example:

perimeters that are passed:

SELECT = "this";
FROM = "that";
WHERE = " 'ID' = 332";

the URL might look like this

www.example.com/rest.php?SELECT=this&FROM=that&WHERE='ID'=332

then in my php script

if (isset($_GET['SELECT']))
{
    $SELECT = $_GET['SELECT'];
}
if (isset($_GET['FROM'])) 
{
    $FROM = $_GET['FROM'];
}
if (isset($_GET['WHERE'])) 
{
    $WHERE = $_GET['WHERE'];
}

So Im thinking that the equals sign in the WHERE statement is messing it up. Would I be correct in this statement?

And if so what might be an alternative?

  • 写回答

1条回答 默认 最新

  • dsa1234569 2012-08-27 01:08
    关注

    RESTful interfaces are about resources that are permalinks. Exposing direct queries over your database does not encapsulate your storage layer and is not RESTful. It's a transparent RPC mechanism over http to query your database (directly by the consumer).

    In your example:

    www.example.com/rest.php?SELECT=this&FROM=that&WHERE='ID'=332
    

    'that' seems to be the resource, 'this' is the data on it and id is the unique reference to that object.

    So, to be more restful, consider:

    www.example.com/api/that/{id}
    

    The data returned contains the 'columns' or attributes

    {
        attr1:val1,
        attr2:val2
    }
    

    Your server can map that/{id} to a method which takes the id, formulates the necessary sql query, get's the tabular data, populates an objects and returns it (serializing to json, xml, etc...)

    If you need to further filter, consider querystrng params to control the options (but not a sql where clause).

    www.example.com/api/that/{id}?option=val1&option2=val2
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退