2

We have to implement the punchout concept in magento2.

There is a 3rd party system they will send data using the cXML format to Magento 2 endpoint. We need to fetch that data in the Magento2 & need to process & need to return the data in the form of cXML format.

For this, we have implemented the vie/api/punchout controller concept instead of REST APIs. which is working when we use the PUT method but it is not working for the POST method.

Note: It should be work for the POST method. When we use POST method our controller is not triggering can anyone help me on this issue.

https://drive.google.com/file/d/1b_aQdjNuamo1VLsLzts0nAsuPGUFjl2H/view

https://codeshare.io/alW8lB sample cXML format: https://codeshare.io/2W91Nd

I have followed the below approaches in the controller but those are working for the PUT method, not for the POST method as per the client request, it should work for the POST method.

  1. $postData = $this->getRequest()->getContent();

  2.  $opts = array('http' =>
    
        array(
            'method'  => 'POST'
            )
            );
        $context  = stream_context_create($opts);
    
        $x=file_get_contents("php://input", false, $context);>              
        echo $x;
    
  3.   $rawData = file_get_contents("php://input");
     echo $rawData;
    

Steps to verify:

  1. Create on the custom controller in your custom module ex: vie/api/punchout

  2. Open postman app

  3. Keep sample XML data in the postman app at body and type is XML format which I have mentioned above.

  4. Try to fetch the data using the POST method in the controller

2 Answers 2

3

I also faced same issue my previous project.

Please try with below code it will work for your issue and let me know if you need more help on this.

protected $request;
protected $formKey;

public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Framework\Data\Form\FormKey $formKey,
    \Magento\Framework\App\Request\Http $request
) {
    $this->request = $request;
    $this->formKey = $formKey;
    $this->request->setParam('form_key', $this->formKey->getFormKey());
    parent::__construct(
        $context
    );
}

try to fetch xml data by below code in your execute method

$xmlBody = new \SimpleXMLElement($this->getRequest()->getContent());
1
  • sure @naren i will try and let you know Commented Nov 29, 2019 at 9:05
0

Or just try this, I managed to implement punch out and when I had this same issue I only had to add this bit of code. Do make sure your controller implements \Magento\Framework\App\CsrfAwareActionInterface

/**
 * @param RequestInterface $request
 *
 * @return bool|null
 */
public function validateForCsrf(RequestInterface $request): ?bool
{
    return true;
}

/**
 * @param RequestInterface $request
 *
 * @return InvalidRequestException|null
 */
public function createCsrfValidationException(RequestInterface $request): ?InvalidRequestException
{
    return null;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.