I need to redirect to some external url with specific header from my controller. I tried to use setHeader on my redirect object but it seems my header is not passed cause i receive a 401 response. If i do a test with a guzzle object, just to check, headers are ok. So how can i redirect to an external url passing headers parameters in a controller? My code:
declare(strict_types=1);
namespace Myvendor\Mymodule\Controller\Hipe;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\View\Result\PageFactory;
use \Magento\Customer\Model\Session;
class Redirect implements HttpGetActionInterface
{
/**
* @var PageFactory
*/
protected $resultPageFactory;
private $logger;
private \Magento\Customer\Model\Session $session;
private \Myvendor\Mymodule\Helper\Data $helper;
private \Magento\Framework\Controller\Result\RedirectFactory $redirectFactory;
/**
* Constructor
*
* @param PageFactory $resultPageFactory
*/
public function __construct(\Magento\Framework\Controller\Result\RedirectFactory $resultRedirectFactory, PageFactory $resultPageFactory,\Magento\Customer\Model\Session $session, \Packitoo\Hipe\Helper\Data $helper, \Psr\Log\LoggerInterface $logger)
{
$this->resultPageFactory = $resultPageFactory;
$this->logger = $logger;
$this->session = $session;
$this->helper = $helper;
$this->redirectFactory = $resultRedirectFactory;
}
/**
* Execute view action
*
* @return ResultInterface
*/
public function execute()
{
if ($this->session->isLoggedIn()){
$customerId = $this->session->getCustomer()->getId();
$token = $this->helper->getToken($customerId);
$redirect = $this->redirectFactory->create();
$redirect->setHeader('Authorization', "Bearer " . $token, true);
$redirect->setPath('https://myexternalurl');
return $redirect;
}
}
}
This is off the top of my head and tbh only slightly different. Also have you tries setting a different header eg Content-Type to see if you ccan change anything?
public function __construct(
//...
Magento\Framework\Controller\Result $rawResultFactory,
//...
)
{
$this->rawResultFactory = $rawResultFactory;
}
public function execute()
{
if ($this->session->isLoggedIn()){
$customerId = $this->session->getCustomer()->getId();
$token = $this->helper->getToken($customerId);
$redirect = $this->rawResultFactory->create(\Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT);
$redirect->setHeader('Authorization', "Bearer " . $token, true);
$redirect->setPath('https://myexternalurl');
return $redirect;
}
}
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.