views:

97

answers:

2

I'm using WWW::Mechanize to do some standard website traversal, but at one point I have to construct a special POST request and send it off. All this requires session cookies.

In the POST request I'm making, spaces are being encoded to + symbols, but I need them encoded as a %20.

I can't figure out how to alter this behaviour. I realise that they are equivalent, but for reasons that are out of my hands, this is what I have to do.

Thanks for any help.

A: 

This appears to be hardcoded in URI::_query::query_form(). I'd conditionally modify that based on a global as is done with $URI::DEFAULT_QUERY_FORM_DELIMITER and submit your change to the URI maintainer.

Other than that, perhaps you could use a LWP::UserAgent request_prepare callback handler?

ysth
Why do you think $URI::DEFAULT_QUERY_FORM_DELIMITER has anything to do with it? That's the thing that goes between pairs. This is an issue with query_form hard-coding the s/ /+/g;
brian d foy
@brian d foy: I don't. I was suggesting doing a similar hack for overriding URI's space encoding.
ysth
+1  A: 

This is hard-coded in URI::_query::query_form(). It translates the spaces to +.

 $val =~ s/ /+/g;

It then calls URI::_query::query with the joined pairs, where the only + signs should be encoded spaces. The easiest thing to do is probably to intercept calls to URI::_query::query with Hook::LexWrap, modify the argument before the call starts so you can turn + into %20, and go on from there.

A little bit more annoying would be to redefine URI::_query::query. It's not that long, and you just need to add some code at the beginning of the subroutine to transform the arguments before it continues.

Or, you can fix the broken parser on the other side. :)

I have a couple chapters on dealing with method overriding and dynamic subroutines in Mastering Perl. The trick is to do it without changing the original source so you don't introduce new problems for everyone else.

brian d foy
Much better to fix it in a way others can use and isn't silently broken if someone does something like translating URI to XS.
ysth
Thanks for the info. I'm going to have to learn a bit more perl...
aidan