简体   繁体   中英

Magento: Receive XML Post Data

In Magento, im trying to setup a route / controller that will receive XML POST data, process it, and return a response.

My route is setup properly, and my Index Controller is setup with an indexAction(). However, using Postman, when I try and POST XML data to the route, Mage::app()->getRequest()->getPost() returns empty. I've also tried $this->getRequest()->getParams() as well with the same results.

Is there something I'm missing?

getRequest()->getPost() is a wrapper for $_POST variable
and $_POST is set for:
Content-Type: application/x-www-form-urlencoded
In other words, for standard web forms (sending params like username=admin&pass=mypass)

$_POST is NOT set for:
Content-Type:text/xml
so you will not get your xml in $_POST.

getRequest()->getParams() contains $_POST, $_GET, and route parameters, again you will not get your xml here.

You can check Zend_Controller_Request_Http class for those methods.

You must parse yourself the xml posted. You can retrieve it like this

if ($this->getRequest()->isPost() && $this->getRequest()->getHeader('Content-Type') == 'text/xml') { // don't forget to set proper content-type header when making the request
    $postedXml = $this->getRequest()->getRawBody();
    if (false !== $postedXml) {
        // process xml
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM