The response of a REST resource has the root element set to always. Is there a way to change this. Its set in \Symfony\Component\Serializer\Encoder\XmlEncoder or JsonEncoder
I have created a custom rest resource.
The response of a REST resource has the root element set to always. Is there a way to change this. Its set in \Symfony\Component\Serializer\Encoder\XmlEncoder or JsonEncoder
I have created a custom rest resource.
Comments
Comment #2
abhishek-anand commentedComment #3
wim leersComment #4
abhishek-anand commentedThis is a requirement for my custom rest resource.
I am using an XMLEndpoint.
No, I am using core rest module. I tagged it incorrectly.
Comment #5
wim leersCan you explain why you're using XML? Like I said, chances are we'll drop XML support. You're the very first user I encounter who actually is using the
xmlformat.Now that I have the necessary background info, I can look into this and formulate a proper answer. In the mean time, please answer the above question.
Comment #6
abhishek-anand commentedI am working with a client who is migrating from a different platform to drupal, maintaining their previous api endpoints. We have no choice but to generate XML endpoints, otherwise their frontend which is consuming those endpoints will break.
Comment #7
wim leersThanks, that's a very good reason!
Comment #8
wim leersUsing a different root node for the
xmlformat is equivalent with using a custom format. So, you must specify your own format. Let's assume it's calledfoo_xml, and the root node should befoo.Then you'll want to do something like this, in a custom module:
And this in its
*.services.ymlfile:Then you can interact with your custom REST resource via
http://example.com/your/custom/rest/resource?_format=foo_xml… and the root node will no longer be<response>, but<foo>.Comment #9
abhishek-anand commentedThis approached worked for me. Thanks a lot.
Comment #10
wim leersGreat! :)
Comment #11
swentel commentedfwiw, I had this need as well, I just added an annotation on my resource and the used that in the RequestHandler which I overtook. (the things you have todo migrating from D6 services to D8 isn't always pleasant, but oh well)
Comment #12
wim leersThis approach will be more reliable. Formats will continue to work the same way, they're an API. A particular controller is not an API; it may change between releases (and in the case of
RequestHandler, it already has, and certainly will again in the future), which means you'll need to chase changes.Comment #14
duncan.moo commentedFor the sake of reference and assisting anyone viewing this page, XmlEncoder includes a method allowing you to change the XML root node name:
gives you:
Comment #15
guilmour-asc commentedWorked perfectly...
But how do I add context to my new format, just like "xml_format_output" or "xml_encoding"?
Comment #16
bbuchert commented@guilmour-asc this might help: https://stackoverflow.com/questions/47523962/how-set-encoding-utf-8-and-...