Home > other >  Trying to create Dancer2 custom response
Trying to create Dancer2 custom response

Time:12-15

I'm currently trying to create a custom dancer response so that I can return my result in json, however when I run the following code, I get the error:

Can't locate object method "new" via package "Dancer::Response" (perhaps you forgot to load "Dancer::Response"?) at app.pl line 11./

Here is the code:

 #!/usr/bin/perl
use warnings;
use strict;

use Dancer2;
require './commacounter.pl';

print "Running";

Dancer::Response->new(
    status => 200,
    content => 'null'
);

my $response = Dancer2::SharedData->response;

Dancer2::SharedData->response->status; #200

get '/text=:string' => sub {
    $response->content_type('application/json');
    $response->content(commacounter(params->{string}));
    return $response;
};

any qr{.*} => sub {
    status 'not_found';
    template 'special_404', { path => request->path};
};

dance;

Anyone know why I'm getting an error, or if there's a better way to do this? Many thanks!

CodePudding user response:

Dancer::Response module not loaded. Just type:

use Dancer::Response;

CodePudding user response:

Dancer::Request is part of the original Dancer framework. If you're using Dancer2, then you should be using Dancer2::Core::Response instead.

  • Related