使用Luracast Restler - 将POST变量作为JSON传递到正文中

时间:2011-10-12 22:07:30

标签: php json post

为一个项目评估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输入视为关联数组...

为了完整性,这里是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 ......

谢谢!

1 个答案:

答案 0 :(得分: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 -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"
  }
}

现在让我们回到在帖子请求的主体中发送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"
  }
}

发布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发布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
  }
}