【问题标题】:Catalyst::Controller::REST returns 415 unsupported media typeCatalyst::Controller::REST 返回 415 不支持的媒体类型
【发布时间】:2014-01-30 05:01:02
【问题描述】:

我想通过 Catalyst 编写 restful api,并用于这个 [Catalyst::Controller::REST][1]。

我已经写了那个代码。

package addressbook::Controller::REST;
use strict;
use warnings;
use base qw(Catalyst::Controller::REST);


sub user :Local :ActionCLass('REST') :Args(1){
    my( $self, $c, $id ) = @_;

    $c->stash( id => $id );

}

# Get user
sub user_GET {
    my ( $self, $c ) = @_;

    my $user = $c->model('DB::User')->find( { id => $c->stash->{id} } );

    if ( $user ){
            $self->status_ok($c, entity => { firstname => $user->firstname } ); 
    }
    else {
            $self->status_not_found($c, message => 'No matching user'); 
    }
}

__PACKAGE__->config(default => 'text/x-json');
1;

然后我运行服务器,转到 localhost:3000/rest/user/1(我有那个 id 的用户)并获取

找不到您的客户端支持的 Content-Type。

我尝试设置 PACKAGE->config application/json, text/xml, text/html, text/x-yaml ...但没有帮助。

有什么想法吗?

谢谢。

【问题讨论】:

    标签: perl rest catalyst


    【解决方案1】:

    在实施时,Catalyst Action REST 对请求进行内容协商以确定要使用的序列化方法。默认设置只是一个后备,通常您在现实世界中的请求将包含一个内容类型。

    有关支持的内容类型以及如何在新的反序列化器中映射的文档可在此处找到: enter link description here。另请注意,最近的版本删除了对 YAML 的内置支持 如果您只是在浏览器中请求 url,这将是对 text/html 的默认响应。

    使用 curl 或使用 Javascript 在真实浏览器中进行测试

    curl -H "Content-Type: application/json" http://localhost:3000/rest/user/1
    

    同时检查您安装的 Catalyst 版本,该版本会在您启动服务器时出现在信息行中。

    【讨论】:

    • 谢谢。我在文档中详细翻找并解决了问题
    猜你喜欢
    • 2014-09-15
    • 1970-01-01
    • 2015-10-25
    • 2021-04-27
    • 2016-11-26
    • 2012-06-20
    • 2016-11-21
    • 1970-01-01
    • 2014-05-10
    相关资源
    最近更新 更多