[英]Using Luracast Restler — passing POST vars as JSON in body
Evaluating Lurasoft RESTler for a project and got stuck on their examples -- trying to pass JSON structure through the body of a post request...I've a working set-up with passing data via the URL but I wanted to get this post-via-body data figured out: 为一个项目评估Lurasoft RESTler并停留在他们的示例上-尝试通过POST请求的正文传递JSON结构...我有一个工作设置,可以通过URL传递数据,但我想得到这篇文章-via-body数据计算出:
So, I have a simple method for handling POST requests defined within the UserAccount class: 因此,我有一个简单的方法来处理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)));
}
And I invoke with curl: 我用curl调用:
curl -X POST http://ll2api/userprofile -H "Content-Type: application/json" -d '{"email_pro" : "mshallop@nileguide.com"}'
I get returned, every time: 每次我都会回来:
{
"error": {
"code": 204,
"message": "No Content: requestData is null"
}
}
If I use the tool LuraCast recommends on their website to generate the REST request, RESTConsole v 4.0.2, I see where my data is being received when the payload is being defined using the "request parameters" section as shown in the request body: 如果我使用LuraCast在其网站上推荐的工具来生成REST请求RESTConsole v 4.0.2,则可以看到使用“请求参数”部分定义有效负载时在哪里接收了我的数据,如请求正文中所示:
Request Url: http://ll2api/userprofile
Request Method: POST
Status Code: 200
Params: {
"email_pro": "mshallop@gmail.com"
}
Finally, from reading their example, I see where the input parameter "$requestData" is broken down as an associative array in their validate() method, treating the JSON input as an associative array... 最后,通过阅读他们的示例,我看到输入参数“ $ requestData”在他们的validate()方法中被分解为一个关联数组,将JSON输入视为一个关联数组...
For completeness, here's the REQUEST headers: 为了完整起见,以下是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
So, tl;dr: To avoid clogging-up the URL of the REST request, how do I pass JSON data via the body of a POST request to LuraCast RESTler and pick-up said variables within the method? 因此,tl; dr:为避免阻塞REST请求的URL,如何通过POST请求的主体将JSON数据传递给LuraCast RESTler,并在方法中提取所述变量?
reference site: http://help.luracast.com/restler/examples/_006_crud/readme.html 参考网站: http : //help.luracast.com/restler/examples/_006_crud/readme.html
Appreciate any help - this is my first run at REST so I suspect my issue is pbck/nub... 感谢任何帮助-这是我第一次在REST上运行,所以我怀疑我的问题是pbck / nub ...
thanks! 谢谢!
I have modified your class to make sure it works, and makes understanding it easy 我已经修改了您的课程,以确保它能正常工作,并且使理解变得容易
<?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);
}
}
First notice that the name of the parameter variable is important, only when you set it as $request_data
it will pass all the parameters passed in 首先请注意,参数变量的名称很重要,只有将其设置为
$request_data
它才会传递传入的所有参数
curl -d '' http://restler2.dev/test/post_example/index.php/useraccount
returns 退货
{
"error": {
"code": 412,
"message": "Precondition Failed: requestData is empty"
}
}
curl -d 'email=arul@luracast.com' http://restler2.dev/test/post_example/index.php/useraccount
returns 退货
{
"received": {
"email": "arul@luracast.com"
}
}
curl -X POST http://restler2.dev/test/post_example/index.php/useraccount -H "Content-Type: application/json" -d '{"email_pro" : "mshallop@nileguide.com"}'
returns 退货
{
"received": {
"email_pro": "mshallop@nileguide.com"
}
}
Hope this makes it clear. 希望这一点可以弄清楚。
In case you want to use your own variable name, this is how it will work 如果您想使用自己的变量名,这就是它的工作方式
<?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
returns 退货
{
"error": {
"code": 412,
"message": "Precondition Failed: email is null"
}
}
curl -d 'email=arul@luracast.com' http://restler2.dev/test/post_example/index.php/useraccount
returns 退货
{
"received": {
"email": "arul@luracast.com",
"age": null
}
}
curl -X POST http://restler2.dev/test/post_example/index.php/useraccount -H "Content-Type application/json" -d '{"email" : "mshallop@nileguide.com"}'
returns 退货
{
"received": {
"email": "mshallop@nileguide.com",
"age": null
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.