繁体   English   中英

使用Luracast Restler —在主体中将POST vars作为JSON传递

[英]Using Luracast Restler — passing POST vars as JSON in body

为一个项目评估Lurasoft RESTler并停留在他们的示例上-尝试通过POST请求的正文传递JSON结构...我有一个工作设置,可以通过URL传递数据,但我想得到这篇文章-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在其网站上推荐的工具来生成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 ...

谢谢!

我已经修改了您的课程,以确保它能正常工作,并且使理解变得容易

<?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"
  }
}

现在让我们回到在请求后的正文中发送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
  }
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM