最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Symfony 7 returns duplicate response - Stack Overflow

programmeradmin1浏览0评论

I need some help with a strange problem on Symfony 7.1 that i can not figure out. I set up locally a Symfony project and run with Symfony build-in server on a MacOS host machine, and works just great

{ 
  "my_response" : [
      {
         "attribute" : "value1"
      },
      {
         "attribute" : "value2"
      }
   ]
}

However, the exact same project on a Windows Host machine (at first place) and eventually deployed to a Linux server online, outputs the following result

{ 
  "my_response" : [
      {
         "attribute" : "value1"
      },
      {
         "attribute" : "value2"
      }
   ]
}
{ 
  "my_response" : [
      {
         "attribute" : "value1"
      },
      {
         "attribute" : "value2"
      }
   ]
}

My controller / action looks like this

#[Route('/pathto/apiresponse', name: 'path_to_api')]
public function apiresponse(ApiResponse $apiResponse): Response
{

  $response = new Response();

  $response->setContent(json_encode($apiResponse->retrieveResponse()));

  $response->setStatusCode(Response::HTTP_OK);

  $response->headers->set('Content-Type', 'application/json');

  return $response->send();

}

I have already seen this Symfony json response is returning content twice but i believe my case differs somehow, since it has different response on different environments

I need some help with a strange problem on Symfony 7.1 that i can not figure out. I set up locally a Symfony project and run with Symfony build-in server on a MacOS host machine, and works just great

{ 
  "my_response" : [
      {
         "attribute" : "value1"
      },
      {
         "attribute" : "value2"
      }
   ]
}

However, the exact same project on a Windows Host machine (at first place) and eventually deployed to a Linux server online, outputs the following result

{ 
  "my_response" : [
      {
         "attribute" : "value1"
      },
      {
         "attribute" : "value2"
      }
   ]
}
{ 
  "my_response" : [
      {
         "attribute" : "value1"
      },
      {
         "attribute" : "value2"
      }
   ]
}

My controller / action looks like this

#[Route('/pathto/apiresponse', name: 'path_to_api')]
public function apiresponse(ApiResponse $apiResponse): Response
{

  $response = new Response();

  $response->setContent(json_encode($apiResponse->retrieveResponse()));

  $response->setStatusCode(Response::HTTP_OK);

  $response->headers->set('Content-Type', 'application/json');

  return $response->send();

}

I have already seen this Symfony json response is returning content twice but i believe my case differs somehow, since it has different response on different environments

Share Improve this question asked Nov 16, 2024 at 20:07 nikolasnikolas 7223 gold badges18 silver badges38 bronze badges 5
  • 1 Any reason for not using new JsonResponse()? – Nico Haase Commented Nov 18, 2024 at 9:31
  • It was a bad call as it turned out. It would have saved me from all this trouble, and investigation. – nikolas Commented Nov 18, 2024 at 9:33
  • This question is similar to: Symfony json response is returning content twice. You even didn't try to delete your $response->send(); as said in this question's answer. – Michael Sivolobov Commented Nov 19, 2024 at 18:11
  • I mentioned in my post that I had already visited the suggested post prior posting here, and although it seemed similar, I believe it differed, since i didn't return the entire response object as mentioned in the related post, but just the $response->send(). – nikolas Commented Nov 21, 2024 at 7:59
  • I did try the solution and I explained why I believe it differed. I don’t understand why you become hostile. – nikolas Commented Nov 21, 2024 at 19:41
Add a comment  | 

1 Answer 1

Reset to default -2

I figured out what was going on afterall.

Symfony's Response send() method is as follows (ref: https://github/symfony/symfony/blob/7.2/src/Symfony/Component/HttpFoundation/Response.php )

/**
 * Sends HTTP headers and content.
 *
 * @param bool $flush Whether output buffers should be flushed
 *
 * @return $this
 */
public function send(bool $flush = true): static
{
    $this->sendHeaders();
    $this->sendContent();

    if (!$flush) {
        return $this;
    }

    if (\function_exists('fastcgi_finish_request')) {
        fastcgi_finish_request();
    } elseif (\function_exists('litespeed_finish_request')) {
        litespeed_finish_request();
    } elseif (!\in_array(\PHP_SAPI, ['cli', 'phpdbg', 'embed'], true)) {
        static::closeOutputBuffers(0, true);
        flush();
    }

    return $this;
}

My stack had fastcgi_finish_request enabled and had as a result to have the "correct" response output, while on the other environments that wasn't enabled, there was the incident with the duplicate output.

If i had used JsonResponse in the first place i wouldn't have come across this issue.

发布评论

评论列表(0)

  1. 暂无评论