1
votes

I have a problem with put in kohana restfull api. It's looks like my $post doesn't work, because after i use this function i can only see 'Data not found!'. Any idea? Thanks for help. Here is my code.

$trg_id = $this->request->param('id');
        if ($trg_id) {
            $post = $this->request->post();
            if ($post) {
                $objTrackingGroup = ORM::factory('Orm_trackingGroup', $trg_id);
                if ($objTrackingGroup->loaded()) {
                    $objTrackingGroup->values($post)
                                      ->save();
                    $this->rest_output($data, 200);
                        } else {
                            $data = array(
                                'code' => 500,
                                'error' => 'Unknown error'
                            );
                            $this->rest_output($data, 500);
                        }

                } else {
                    $data = array(
                        'code' => 404,
                        'error' => 'Data not found!'
                    );
                    $this->rest_output($data, 404);
                }

        }else {
            $data = array(
                'code' => 404,
                'error' => 'Data not found'
            );
            $this->rest_output($data, 404);
        }
1
It's seems like update function cannot see post.R0ck99
Are you submitting data using PUT or POST method? because $this->request->post(); will only work with POST.Faraz

1 Answers

0
votes

You can use this Kohana module to work with Restfull API. it has methods to deal with PUT, UPDATE methods https://github.com/SupersonicAds/kohana-restful-api

You can use

$this->request->query (); 
$this->request->body ();

If you receive your Parameters in query() then its a array you can use it, or If you request is RAW request in body() than you can use parse_str function to convert it into array.

Here is my example

class Controller_RestTest extends Controller_REST {
    public function action_index() {
    }
    public function action_update() {
        echo Debug::vars ( $this->request->query () );
        echo Debug::vars ( $this->request->body () );
    }
}

and for this PUT request I get this response

PUT http://localhost/RestTest?a=1234&b=4567
Content-Type: application/x-www-form-urlencoded
?body-contents=this is boy contents
 -- response --
200 OK
Server:  nginx
Date:  Tue, 11 Oct 2016 11:43:44 GMT
Content-Type:  text/html; charset=utf-8
Content-Length:  158
Connection:  keep-alive
Cache-Control:  no-cache, no-store, max-age=0, must-revalidate
Vary:  Accept-Encoding
Content-Encoding:  gzip
X-Powered-By:  PHP/5.6.26




array(2) (
    "a" => string(4) "1234"
    "b" => string(4) "4567"
)

string(35) "?body-contents=this is boy contents"