I'm successfully using Dancer2::Plugin::REST to return JSON from my web server when users access mydomain.com/api/1.json .While retaining this feature, how can I use Dancer2::Plugin2::REST to return XML when users access mydomain.com/api/1.xml ?
My config.yml contains this section:
serializers:
json: JSON
yml: YAML
yaml: YAML
dump: Dumper
xml: XML
My lib/MyApp/API.pm looks like this:
package MyApp::API;
use Dancer2;
use Dancer2::Plugin::REST;
prepare_serializer_for_format;
get '/1.:format' => sub {
return { 'temperature' => '10', 'date' => '2019-09-01' };
};
true;
As expected I get the correct JSON snippet when running:
curl -H 'Accept-Type: text/json' https://<mydomain>/api/1.json
But when I run:
curl -H 'Accept-Type: text/xml' https://<mydomain>/api/1.xml
or
curl -H 'Accept-Type: application/xml' https://<mydomain>/api/1.xml
or
curl https://<mydomain>/api/1.xml
I get a server error in response:
Internal Server Error
Reference {"exception" => "Reference {\"date\" => \"2019-09-01\",\"tem...} did not pass type constraint "Str" (in $self->{"content"}) at /usr/local/share/perl/5.26.1/Dancer2/Core/Response.pm line 122
"Str" is a subtype of "Value"
Reference {"exception" => "Reference {\"date\" => \"2019-09-01\",\"tem...} did not pass type constraint "Value" (in $self->{"content"})
"Value" is defined as: (defined($_) and not ref($_))
I expected to get a XML response, not a server error. What am I missing?