Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Luracast Restler -- passing POST vars as JSON in body

Tags:

json

post

php

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:

So, I have a simple method for handling POST requests defined within the UserAccount class:

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 -X POST http://ll2api/userprofile -H "Content-Type: application/json" -d '{"email_pro" : "[email protected]"}'

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:

Request Url: http://ll2api/userprofile
Request Method: POST
Status Code: 200
Params: {
    "email_pro": "[email protected]"
}

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...

For completeness, here's the REQUEST headers:

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?

reference site: 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...

thanks!

like image 815
Micheal Shallop Avatar asked Oct 12 '11 22:10

Micheal Shallop


1 Answers

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

Lets try few curl examples now

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

returns

{
  "error": {
    "code": 412,
    "message": "Precondition Failed: requestData is empty"
  }
}

Next let me pass some data using standard http post vars

curl -d '[email protected]' http://restler2.dev/test/post_example/index.php/useraccount

returns

{
  "received": {
    "email": "[email protected]"
  }
}

Now lets get back to sending JSON data in body of post request

curl -X POST http://restler2.dev/test/post_example/index.php/useraccount -H "Content-Type: application/json" -d '{"email_pro" : "[email protected]"}'

returns

{
  "received": {
    "email_pro": "[email protected]"
  }
}

Hope this makes it clear.


Modified example

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 for failure result

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

returns

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

curl for posting http vars

curl -d '[email protected]' http://restler2.dev/test/post_example/index.php/useraccount

returns

{
  "received": {
    "email": "[email protected]",
    "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" : "[email protected]"}'

returns

{
  "received": {
    "email": "[email protected]",
    "age": null
  }
}
like image 64
2 revs Avatar answered Oct 21 '22 14:10

2 revs