I followed documentation of symfony to use httpClient to communicate between two servers but I’m facing an access denied problem:
Access Denied : {“parameter1″:”test”,”parameter2″:”test2″} [] {“token”:{“username”:”anon.”,”authenticated”:true,”roles”:[]}}
my code :
public function buildHeader(): array
{
return [
"ftapplicationroles" => "ADMIN",
"sec-fetch-user" => "?1",
"connection" => "keep-alive",
"accept-encoding" => "gzip, deflate, br",
"accept-language" => "fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3",
"accept" => "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
"host" => "serverUrl",
"sm-universalid" => "TEST2024",
"sm-authtype" => "form",
];
}
/**
* @Route("/default/send", name="test_send")
*/
public function sendAction(Request $request): Response
{
$data = ['parameter1'=> 'test', 'parameter2'=> 'test2'];
$json = json_encode($data);
$sendHeaders = $this->buildHeader();
$token = $this->tokenStorage->getToken();
try {
$response = $this->client->request(
'POST',
'https:serverUrl/default/insert',
array(
'query' => ['token' => $token,],
'body' => $json,
'headers' => $sendHeaders,
'timeout' => 30
)
);
// Getting the content of the response
$content = $response->getContent();
var_dump($content);
} catch (TransportExceptionInterface $e) {
dd($e->getMessage());
}
return new Response('data sent');
}
/**
* @Route("/default/insert", name="insert_action")
*/
public function insertAction(Request $request): Response
{
$data = json_decode($request->getContent(), true);
// Handle data, perform any necessary operations
$configuration = new Configuration();
$configuration->setComment('new line inserted..');
$configuration->setParam('parameter1');
$configuration->setParam2('parameter2');
$this->entityManager->persist($configuration);
$this->entityManager->flush($configuration);
return new Response('data inserted successfully');
}
What I’m missing in my code?
Thanks in advance.