使用Luracast Restler -- 将POST变量作为JSON传递在请求体中

5

我正在评估Lurasoft RESTler是否适用于一个项目,但在他们的示例中遇到了问题--尝试通过post请求的主体传递JSON结构...我已经通过URL传递数据建立了一个工作设置,但我想弄清楚如何通过主体进行数据传输:

因此,在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网站推荐的工具来生成REST请求,如RESTConsole v 4.0.2,在请求体中使用“请求参数”部分定义有效负载时,我可以看到数据被接收的位置:
Request Url: http://ll2api/userprofile
Request Method: POST
Status Code: 200
Params: {
    "email_pro": "mshallop@gmail.com"
}

最后,从阅读他们的示例中,我看到输入参数"$requestData"在他们的validate()方法中被拆分为一个关联数组,将JSON输入视为一个关联数组...

为了完整起见,这是请求头:

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

因此,总之:为了避免 REST 请求的 URL 被堵塞,我应该如何通过 POST 请求的正文传递 JSON 数据到 LuraCast RESTler,并在方法中获取这些变量?
参考网站:http://help.luracast.com/restler/examples/_006_crud/readme.html 非常感谢您的帮助 - 这是我第一次尝试 REST,所以我怀疑我的问题是 pbck/nub...
谢谢!
1个回答

6
我已经修改了你的代码使之运行正常,并且更容易理解。
<?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变量传递一些数据。
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请求失败的结果

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

返回值

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

curl for posting http vars

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

返回

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

curl to post 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是一个保留变量的推断...遵循我们商店命名传递参数的惯例肯定是问题所在...当我开始尝试认证时,可能会再次“见到”你...:) - user302761
谢谢你的见解。我也卡在那个例子里了。 问题:我可以有一个方法,通过post接收参数,但不将其命名为post()或postSomething()吗? - JulianG

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接