0

Strange behavior with passing defaults={"_format" = "xml"} to controller (seemed to work before):

/**
 * @Route("/orderxml/{orderguid}", name="_show_order_xml", defaults={"_format" = "xml"})
 */
public function showOrderXML($orderguid)
{
....
$xmloutput = $this->container->get('templating')
        ->render($templateName, $tpl_data);
$response = new Response($xmloutput);
}

Though I pass defaults={"_format" = "xml"}, the response is still received with content-type=text/html.

Tried to debug the Request - it comes with empty Content-Type header. Attributes of Request do contain _format=xml, but also contain media-type="text/html", which is not familiar to me. As stated in the docs, _format determines the content-type of Request and Response objects.

Currently the only thing I could do is $response->headers->set('Content-Type', 'text/xml');

How can this be fixed?

P.S.: symfony 2.3

1
  • Where did you read that _format applies to the response? Pretty sure it only applies to the request. Manually setting the header is the easiest option. Or you could make an XmlResponse object using JsonResponse as a guide. Commented Feb 14, 2015 at 20:13

1 Answer 1

1

_format define the content-type, but you set it only as default. Since the request goes with contant-type html, the default doesn't matter. you have also to set the _format in the requirements to xml only too.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, that was the problem. Btw, if my request was sent with content-type=application/json, I assume it would return json and requirements does not need to be set?
Yes. But requirements are good to accept e.g. only json and xml

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.