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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀