2

I have a Catalyst Controller that responds to arguments in the usual way (/endpoint/foo/bar), but also takes a query parameter for a security token. How can I set the value of the parameter before I initiate a forward call to this Controller? It doesn't let me assign to $c->req->param('token').

(It's not possible to rewrite the controller to accept the token as an argument instead of a parameter.)

user1235777
  • 609
  • 7
  • 24
  • You are saying you want to modify the request, so you can use this 3rd party Controller correctly? Because it doesn't take Controller args. Is that correct? – simbabque Nov 19 '19 at 14:21
  • Not exactly, but it more or less needs to work this way. It does take Controller args but it also takes a single query parameter. What I actually need to do is initiate a new request for this Controller; I will have both the arguments and the value for the query parameter and I need to get these to the Controller. – user1235777 Nov 19 '19 at 15:02

1 Answers1

2

$c->req->param('token') is not an LValue, so you cannot assign to it. Instead, you need to pass in the value.

$c->req->param('token', 1);

This behaviour is sort of documented:

Like CGI, and unlike earlier versions of Catalyst, passing multiple arguments to this method, like this:

 $c->request->param( 'foo', 'bar', 'gorch', 'quxx' );

will set the parameter foo to the multiple values bar, gorch and quxx. Previously this would have added bar as another value to foo (creating it if it didn't exist before), and quxx as another value for gorch.

Consider this demo:

package MyApp::Controller::Root;
use base 'Catalyst::Controller';
__PACKAGE__->config(namespace => '');

sub default : Path {
    my ($self, $c) = @_;
    $c->req->param('foo', 1);
    $c->forward('bar');
}

sub bar : Path('foo') {
    my ($self, $c) = @_;

    if ($c->req->param('foo')) {
        $c->res->body("Secret param set!\n");
    }
    else {
        $c->res->body("Hello World\n");
    }
    return;
}
simbabque
  • 53,749
  • 8
  • 73
  • 136
  • 1
    Ah, wonderful, thank you. I did see that section of the docs, but it was not only confusing, but littered with warnings about how this is unsafe and will be deprecated, so I didn't look closely enough. – user1235777 Nov 19 '19 at 15:20
  • @user1235777 it's not a great solution, but it has to do I guess. – simbabque Nov 19 '19 at 15:23