magento rest api create method return

I make an rest api module for shiping and order with carrier and tracking code.
The problem is when i cal the method _created() -(Post) it doesn’t return any value. I dont know if the function allow returns or not.

This is my create function

protected function _create($orderData)
{
    $orderId = $this->getRequest()->getParam('order_id');

    if($orderData['incrementId']){
        $incrementId =  $orderData['incrementId'];
    }else{
        $cModel = Mage::getModel('sales/resource_order');
        $incrementId = $cModel->getIncrementId($orderId);
    }

    $order = Mage::getModel('sales/order')
        ->getCollection()
        ->addAttributeToFilter('state', array('neq' => Mage_Sales_Model_Order::STATE_CANCELED))
        ->addAttributeToFilter('increment_id', $incrementId)
        ->getFirstItem();

    if ($order->hasData()) {

        if($order->canShip()){
            $shipment = new Mage_Sales_Model_Order_Shipment_Api();
            $shipmentId = $shipment->create($incrementId);
            $trackid = $shipment->addTrack($shipmentId, 'custom', 'ups', '#111111' );
            return $shipmentId;
        }else{
            $this->_critical(self::RESOURCE_NOT_FOUND);
        }

    }else{
        $this->_critical(self::RESOURCE_NOT_FOUND);
    }

    //return null;
}

The api call:

$resourceUrl = "$apiUrl/shipping/order/1?type=rest";
$oauthClient->fetch($resourceUrl, json_encode($orderData), 'POST', $headers);
print_r($oauthClient->getLastResponse());

I dont know why the response comes empty, even if i errase all the content of the function and just return a value, its comes empty.

Any idea for it?

3 thoughts on “magento rest api create method return”

  1. The answer R.S said is for SOAP instead of REST. You definition is correct.
    The return of create is a URL which will appear in http header “location”. It is not a valid way to show response for _create event. If you want to response something, you should use _multicreate

    if you want to add response data in _multicreate you should use

    $this->getResponse ()->addMessage ( "", 0, array (
                'id' => $quote->getId () 
        ), Mage_Api2_Model_Response::MESSAGE_TYPE_SUCCESS );
    

    And you should retrieve the URL in location to receive the correct things.

    Use this code to get URL correctly.

    $this->_getLocation ( $quote )
    

    Another tricky is you can set the header to make it force redirect

    $this->getResponse ()->setRawHeader ( '"Content-Type" = "application/json"' );
    

    However, at this time, only if the magento install in the root of your domain, or the url will be wrong.

    Hence you can use some code not good but make things work.

    The final code is

    $this->getResponse ()->setRawHeader ( '"Content-Type" = "application/json"' );
    $base_url = Mage::getBaseUrl ( Mage_Core_Model_Store::URL_TYPE_WEB );
    $base_url = substr ( $base_url, 0, strlen ( $base_url ) - 1 );
    return $base_url . $this->_getLocation ( $order );
    

Leave a Reply

Your email address will not be published. Required fields are marked *