Magento – External Request – Invalid Form Key Magento 2.3

form-keymagento2.3

I sent a “POST” request via external server for version magento-2.3

My internal Magento URL cannot verify and response is

"Invalid Form Key. Please refresh the page."

as a summary, i want to reach to magento-2.3 from outside and is there any valid way to do it ?

Additional information

I'm planning to build a payment module for my Magento store,
So my PSP working with this way;

When the customer clicks pay button of javascript embeded payment form, my PSP POSTing a value to my provided callbackUrl which is my controller inside of my module. (So I'm expecting a POST value to my controller.

It looks like this: https://mywebsite.com/mymodule/response).

When I'm trying to grab the value with this code piece
($postData = $this->getRequest()->getPostValue(); $postData['token']).

When I'm trying to do this, Magento is requesting a value named "form_key". In earlier version there was no usage like this. I'm suspecting there is a change in the Magento 2.3.0.

Can you help me ?

Best Answer

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
    );
}

This is an fix, which send form_key in all the requests. The above code will work on Magento 2.3 and below Magento 2.3.