Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend Rest Api: putAction on POST method

I got a weird issue. I hope someone could help me.

I am new to Zend and I'm writing a RESTfull API.

When I run the curl command as POST method, it calls the putAction() function.

For example, I am run a curl command:

curl -X POST http://localhost/ws/user/post -v

Here is the response:

* About to connect() to localhost port 80 (#0)
*   Trying 127.0.0.1...
* connected
* Connected to localhost (127.0.0.1) port 80 (#0)
> POST /ws/user/post HTTP/1.1
> User-Agent: curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8r zlib/1.2.5
> Host: localhost
> Accept: */*
> 
< HTTP/1.1 200 OK
< Date: Mon, 22 Oct 2012 13:37:56 GMT
< Server: Apache/2.2.14 (Ubuntu)
< X-Powered-By: PHP/5.3.2-1ubuntu4.18
< Vary: Accept-Encoding
< Content-Length: 53
< Content-Type: text/html
< 
* Connection #0 to host localhost left intact
"From putAction() updating the requested article"
Method = POST
* Closing connection #0

Here is the code:

[...]
public function postAction() {
    echo 'Method = ' . $_SERVER['REQUEST_METHOD'];

    if (!$id = $this->_getParam('id', false)) {
        $this->sendResponse("Bad request. Id is missing", 400);
    }

    $this->sendResponse("From postAction() creating the requested article", 201);
}

public function putAction() {
    echo 'Method = ' . $_SERVER['REQUEST_METHOD'];
    $this->sendResponse("From putAction() updating the requested article");
}
[...]

Any ideas ?


EDIT:

I realized that I put this code in my bootstrap:

public function _initRestRoute() {
    $front = Zend_Controller_Front::getInstance();
    $router = $front->getRouter();

    $restRoute = new Zend_Rest_Route($front, array(), array(
        'ws',
    ));

    $router->addRoute('rest', $restRoute);
}

When I comment it, it works.

Any explanations ? Thanks!

like image 576
Kornikopic Avatar asked Jul 01 '26 14:07

Kornikopic


1 Answers

You shouldn't be having http://localhost/ws/user/post in your request while using the Zend_Rest_Route. This route interprets /user/post as parameters and changes action from post to put. Try http://localhost/ws instead.

See Zend/Rest/Route.php line 208.

like image 164
akond Avatar answered Jul 04 '26 09:07

akond



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!