当用户访问 mydomain.com/api/1.json 时,我成功地使用 Dancer2::Plugin::REST 从我的 Web 服务器返回 JSON。同时保留此功能,我如何使用 Dancer2::Plugin2::REST 返回用户访问 mydomain.com/api/1.xml 时的 XML ?
我的 config.yml 包含此部分:
serializers:
json: JSON
yml: YAML
yaml: YAML
dump: Dumper
xml: XML
Run Code Online (Sandbox Code Playgroud)
我的 lib/MyApp/API.pm 看起来像这样:
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;
Run Code Online (Sandbox Code Playgroud)
正如预期的那样,我在运行时得到了正确的 JSON 片段:
curl -H 'Accept-Type: text/json' https://<mydomain>/api/1.json
Run Code Online (Sandbox Code Playgroud)
但是当我运行时:
curl -H 'Accept-Type: text/xml' https://<mydomain>/api/1.xml
Run Code Online (Sandbox Code Playgroud)
或者
curl -H 'Accept-Type: application/xml' https://<mydomain>/api/1.xml
Run Code Online (Sandbox Code Playgroud)
或者
curl https://<mydomain>/api/1.xml
Run Code Online (Sandbox Code Playgroud)
我收到服务器错误响应:
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($_))
Run Code Online (Sandbox Code Playgroud)
我希望得到 XML 响应,而不是服务器错误。我错过了什么?