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 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 ARIMA模型时间序列预测用pathon解决
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序,怎么查看客户esp32板子上程序及烧录地址
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)