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

Mic*_*lop 5 php post json

为一个项目评估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)));
}
Run Code Online (Sandbox Code Playgroud)

我用curl调用:

curl -X POST http://ll2api/userprofile -H "Content-Type: application/json" -d '{"email_pro" : "mshallop@nileguide.com"}'
Run Code Online (Sandbox Code Playgroud)

我每次都会回来:

{
  "error": {
    "code": 204,
    "message": "No Content: requestData is null"
  }
}
Run Code Online (Sandbox Code Playgroud)

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

Request Url: http://ll2api/userprofile
Request Method: POST
Status Code: 200
Params: {
    "email_pro": "mshallop@gmail.com"
}
Run Code Online (Sandbox Code Playgroud)

最后,通过阅读他们的示例,我看到输入参数"$ 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
Run Code Online (Sandbox Code Playgroud)

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

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

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

谢谢!

Aru*_*ran 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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

让我们现在尝试几个卷曲的例子

curl -d '' http://restler2.dev/test/post_example/index.php/useraccount
Run Code Online (Sandbox Code Playgroud)

回报

{
  "error": {
    "code": 412,
    "message": "Precondition Failed: requestData is empty"
  }
}
Run Code Online (Sandbox Code Playgroud)

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

curl -d 'email=arul@luracast.com' http://restler2.dev/test/post_example/index.php/useraccount
Run Code Online (Sandbox Code Playgroud)

回报

{
  "received": {
    "email": "arul@luracast.com"
  }
}
Run Code Online (Sandbox Code Playgroud)

现在让我们回到在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"}'
Run Code Online (Sandbox Code Playgroud)

回报

{
  "received": {
    "email_pro": "mshallop@nileguide.com"
  }
}
Run Code Online (Sandbox Code Playgroud)

希望这说清楚.


修改的例子

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

<?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));
    }
}
Run Code Online (Sandbox Code Playgroud)

卷曲失败的结果

curl -d '' http://restler2.dev/test/post_example/index.php/useraccount
Run Code Online (Sandbox Code Playgroud)

回报

{
  "error": {
    "code": 412,
    "message": "Precondition Failed: email is null"
  }
}
Run Code Online (Sandbox Code Playgroud)

curl发布http vars

curl -d 'email=arul@luracast.com' http://restler2.dev/test/post_example/index.php/useraccount
Run Code Online (Sandbox Code Playgroud)

回报

{
  "received": {
    "email": "arul@luracast.com",
    "age": null
  }
}
Run Code Online (Sandbox Code Playgroud)

curl发布JSON

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

回报

{
  "received": {
    "email": "mshallop@nileguide.com",
    "age": null
  }
}
Run Code Online (Sandbox Code Playgroud)