Como fazer uma consulta fetch a uma API Rest feita com Symfony
Tenho uma Api rest feita com Symfony. Esta api retorna consultas no formato json.
Tudo foi testado no ambiente localhost:8080.
Esta é uma controller que retorna a consulta:
<?php
namespace App\Controller\Animal\Bull;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use App\Entity\Animal\Bull\Bull;
/**
* @Route("/animal")
*/
class BullGetController extends AbstractController
{
/**
* @Route("/bull/getBull/{id}/{idEarring}", methods={"GET"}, name="get_bull")
*/
public function getBull(int $id, int $idEarring): Response
{
$repository = $this->getDoctrine()
->getRepository(Bull::class);
$bull = $repository->findOneBy([
'id' => $id,
'idEarring' => $idEarring
]);
return $this->json([
'idEarring' => $bull->getIdEarring(),
'name' => $bull->getName(),
'breed' => $bull->getBreed(),
'dateOfBirth' => ($bull->getDateOfBirth())->format('Y-m-d'),
'weight' => $bull->getWeight()
]);
}
}
Com pode se ver na imagem consultas feitas a partir do navegador são retornadas respostas.
Quando é feita uma consulta por meio do Fetch javascript o mesmo não ocorre. A resposta obtida não é igual.
'use strict';
let url = 'http://localhost:8080/animal/bull/getBull/2/2';
const promise = fetch(url).then(
(response) => console.log(response)
);
Gostaria de saber o porque disso? Porque a resposta não é igual.
Spoiler

Discussão (2)
Carregando comentários...