【问题标题】:Using Luracast Restler -- passing POST vars as JSON in body使用 Luracast Restler -- 在正文中将 POST 变量作为 JSON 传递
【发布时间】:2011-12-06 12:37:36
【问题描述】:

为一个项目评估 Lurasoft RESTler 并停留在他们的示例上——试图通过发布请求的正文传递 JSON 结构......我有一个通过 URL 传递数据的工作设置,但我想弄清楚这个 post-via-body 数据:

所以,我有一个简单的方法来处理 UserAccount 类中定义的 POST 请求:

function post($_requestData = null) {
    if (is_null($_requestData)) {
        throw new RestException(HTTPCODE_NO_CONTENT, "requestData is null");
    }
    return(array('success' => array('code' => HTTPCODE_OK, 'msg' => $msg)));
}

我用 curl 调用:

curl -X POST http://ll2api/userprofile -H "Content-Type: application/json" -d '{"email_pro" : "mshallop@nileguide.com"}'

我每次都会被退回:

{
  "error": {
    "code": 204,
    "message": "No Content: requestData is null"
  }
}

如果我使用 LuraCast 在其网站上推荐的工具 RESTConsole v 4.0.2 来生成 REST 请求,我会看到在使用“请求参数”部分定义有效负载时接收数据的位置,如请求正文:

Request Url: http://ll2api/userprofile
Request Method: POST
Status Code: 200
Params: {
    "email_pro": "mshallop@gmail.com"
}

最后,通过阅读他们的示例,我看到输入参数“$requestData”在他们的 validate() 方法中被分解为关联数组,将 JSON 输入视为关联数组...

为了完整起见,这里是 REQUEST 标头:

Accept: application/json
Accept-Language: en
Content-Type: application/json
Connection: keep-alive
Origin: chrome-extension: //rest-console-id
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.904.0 Safari/535.7

所以,tl;dr:为避免阻塞 REST 请求的 URL,我如何通过 POST 请求的主体将 JSON 数据传递给 LuraCast RESTler 并在方法中提取所述变量?

参考网站:http://help.luracast.com/restler/examples/_006_crud/readme.html

感谢任何帮助 - 这是我第一次在 REST 上运行,所以我怀疑我的问题是 pbck/nub...

谢谢!

【问题讨论】:

    标签: php json post


    【解决方案1】:

    我已经修改了您的课程以确保它可以正常工作,并且易于理解

    <?php
    class UserAccount
    {
        //$request_data is a reserved name in Restler2.
        //It will pass all the parameters passed as an
        //associative array
        function post ($request_data)
        {
            //$request_data will never be null
            //instead it can be an empty array
            if (empty($request_data)) {
                throw new RestException(412, "requestData is null");
            }
            return array('received'=>$request_data);
        }
    }
    

    首先注意参数变量的名字很重要,只有当你设置为$request_data时才会传递所有传入的参数

    现在让我们尝试几个 curl 示例

    curl -d '' http://restler2.dev/test/post_example/index.php/useraccount
    

    返回

    {
      "error": {
        "code": 412,
        "message": "Precondition Failed: requestData is empty"
      }
    }
    

    接下来让我使用标准的 http post vars 传递一些数据

    curl -d 'email=arul@luracast.com' http://restler2.dev/test/post_example/index.php/useraccount
    

    返回

    {
      "received": {
        "email": "arul@luracast.com"
      }
    }
    

    现在让我们回到在 post 请求正文中发送 JSON 数据

    curl -X POST http://restler2.dev/test/post_example/index.php/useraccount -H "Content-Type: application/json" -d '{"email_pro" : "mshallop@nileguide.com"}'
    

    返回

    {
      "received": {
        "email_pro": "mshallop@nileguide.com"
      }
    }
    

    希望这说明清楚。


    修改示例

    如果您想使用自己的变量名,这就是它的工作方式

    <?php
    class UserAccount
    {
        //$email and $age can be null
        function post ($email, $age)
        {
            //$request_data will never be null
            //instead it can be an empty array
            if (is_null($email)) {
                throw new RestException(412, "email is null");
            }
            return array('received'=>array('email'=>$email, 'age'=>$age));
        }
    }
    

    失败结果的卷曲

    curl -d '' http://restler2.dev/test/post_example/index.php/useraccount
    

    返回

    {
      "error": {
        "code": 412,
        "message": "Precondition Failed: email is null"
      }
    }
    

    curl 用于发布 http 变量

    curl -d 'email=arul@luracast.com' http://restler2.dev/test/post_example/index.php/useraccount
    

    返回

    {
      "received": {
        "email": "arul@luracast.com",
        "age": null
      }
    }
    

    卷曲发布 JSON

    curl -X POST http://restler2.dev/test/post_example/index.php/useraccount -H "Content-Type application/json" -d '{"email" : "mshallop@nileguide.com"}'
    

    返回

    {
      "received": {
        "email": "mshallop@nileguide.com",
        "age": null
      }
    }
    

    【讨论】:

    • 酷 - 感谢您的回复!我已经修改了我的方法,现在从 curl 和 RESTconsole 得到正确/预期的响应,太棒了!我错过了 $request_data 是保留变量的推论......遵循我们商店命名传递参数的约定绝对是问题......当我开始使用身份验证时,我可能会再次“见到”你......: )
    • 感谢您的洞察力。我也被困在那个例子中。问题:我可以有一个通过 post 接收参数但不将其命名为 post() 或 postSomething() 的方法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-05
    • 1970-01-01
    • 2017-10-14
    • 2020-04-11
    • 2017-07-15
    • 1970-01-01
    • 2011-12-15
    相关资源
    最近更新 更多