Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a redirect that passes a value thanks to help from here. It passes one word entries using a Perl action form then redirects value to my ASP action page. The problem is it wont pass two or more word entries. If I enter one word such as OneWord it works great, but if I enter a string like this OneWord NextWord into my form entry it doesnt work and gives me the error:
HTTP Error 400 400 Bad Request Due to malformed syntax, the request could not be understood by the se +rver. The client should not repeat the request without modifications.
Here is the redirect part:
#!/usr/local/bin/perl -w use strict; use CGI qw(:standard); #Perl web CGI module use CGI::Carp qw(fatalsToBrowser); my $query = new CGI; my $name = $query->param("name"); print $query->redirect("http://webserver/action.asp?name=$name");
Please advise how I can get it to accept more than one word in the form entry for my name field.

Replies are listed 'Best First'.
Re: Passing value with two words or more
by Juerd (Abbot) on Mar 11, 2004 at 14:52 UTC

    print $query->redirect("http://webserver/action.asp?name=$name"); Please advise how I can get it to accept more than one word in the form entry for my name field.

    URI-encode it. See URI::Escape, CGI's escape and the FAQ How do I decode or create those %-encodings on the web?.

    Also, consider learning about the tool you use before you actually use it. You don't seem to know anything about HTTP, but you are using it.

    Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

      Three thoughts here.

      First, Juerd is right - you need to properly quote the text contained in $name, and one way to do that is to use URI::Escape.

      Second, for this specific issue, you could get away with simply surrounding $name in the redirect with double quotes, like this:

      print $query->redirect("http://webserver/action.asp?name=\"$name\"" +); OR print $query->redirect(qq!http://webserver/action.asp?name="$name"! +);
      Third, also as Juerd pointed out, and since you've already "use"d CGI, you could use the CGI 'escape' and 'unescape' functions. Read about those either in Lincoln Stein's book 'Official Guide to Programming With CGI.pm' p.199, or by reading the perldocs on the CGI.pm module by doing
      perldoc CGI
      at a command prompt.

      HTH.

        you could get away with simply surrounding $name in the redirect with double quotes

        Care to explain to me why it would then no longer be two words? I can't imagine that this really works. (And if it does, I'd guess it'd be for MSIE only)

        Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

        Thanks all. I tried the escape quotes and it didnt work. I am trying to use the URI::Escape but not sure how to use it. My browser (Netscape 4.77) is the only one that seems to have the problem. The script works with two words when I use IE 5.5 and Netscape 7 but not with Netscape 4.77.
Re: Passing value with two words or more
by dragonchild (Archbishop) on Mar 11, 2004 at 14:53 UTC
    This isn't a Perl problem - it's a browser/webserver problem. The request from the browser isn't being understood by the webserver. I suggest you ask your sysadmin to help you here.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

      The request from the browser isn't being understood by the webserver.

      Correct, but that request is done because this CGI script told the browser to do so :) That still doesn't make it a Perl problem, but it is something that can be solved with Perl.

      Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }