The response I get to an LWP request is application/x-www-form-urlencoded
is it possible convert the text of this to a hash via some object method?
Asked
Active
Viewed 1,811 times
2

xenoterracide
- 16,274
- 24
- 118
- 243
1 Answers
8
# from a HTTP::Response object
my $urlencoded = $response->content;
Vars
inCGI
returns a hash.use CGI qw(); CGI->new($urlencoded)->Vars;
parameters
inPlack::Request
returns aHash::MultiValue
object, which is actually the appropriate data structure for this.use Plack::Request qw(); Plack::Request->new({QUERY_STRING => $urlencoded})->parameters;
param
inAPR::Request
/libapreq2
- not quite a Perl hash, but an XS object with attached Magic whose behaviour is close enough.insert hand-waving here, no libapreq2 available right now for testing
url_params_mixed
inURL::Encode
require URL::Encode::XS; use URL::Encode qw(url_params_mixed); url_params_mixed $urlencoded;
parse_query_string
inCGI::Deurl::XS
use CGI::Deurl::XS 'parse_query_string'; parse_query_string $urlencoded;
query_form
inURI
serves well, too, in a pinch; and so doesquery_form_hash
inURI::QueryParam
.use URI qw(); URI->new("?$urlencoded")->query_form; use URI::QueryParam qw(); URI->new("?$urlencoded")->query_form_hash;
Bonus: also see
HTTP::Body::UrlEncoded
, as used by Catalyst.

daxim
- 39,270
- 4
- 65
- 132
-
though I'm not sure about the last, the problem with these is that they mostly seem to be decoding a request if I'm not mistaken, I'm trying to decode a response. – xenoterracide Mar 13 '12 at 20:09
-
Edited: Munging a HTTP response body into a HTTP request body is trivial, as you can see. – daxim Mar 13 '12 at 21:17
-
yeah, I was just having trouble finding the right methods/syntax. Probably going to use URI since it should already be loaded. – xenoterracide Mar 13 '12 at 21:42