2

I am making a script that POST some XML to another server but I have problems with a plus singn (+). Here is my code:

#!/usr/bin/perl

use strict;
use warnings;
use LWP::UserAgent;

my $XML = qq|
<?xml version="1.0" encoding="UTF-8"?>
<ServiceAddRQ>
<Service code="Ws%2BsuHG7Xqk01RaIxm2L/w1L">
<ContractList>
<Contract>
<Name>CGW-TODOSB2B</Name>
</Contract>
</ContractList>
</Service>
</ServiceAddRQ>
|;

utf8::encode($XML);


my $ua = LWP::UserAgent->new;
$ua->timeout(120);

my $ret = HTTP::Request->new('POST', $XMLurl);
$ret->content_type('application/x-www-form-urlencoded'); 
$ret->content("xml_request=$XML");

my $response = $ua->request($ret);

As you can see in the attribute code the value string have %2B and the other server recive the value "Ws+suHG7Xqk01RaIxm2L/w1L".

How ca i send %2B literal.

Thanks in advance

Welch

matthias krull
  • 4,389
  • 3
  • 34
  • 54
Welcho
  • 35
  • 6

3 Answers3

4

You need to escape all unsafe characters in the content like this:

use URI::Escape;
$ret->content("xml_request=".uri_escape($XML));
Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
  • 1
    Hi Eugene, It works I only change: This $ret->content(uri_escape("xml_request=$XML")); To $ret->content(xml_request=uri_escape($XML)); And works great. – Welcho Mar 22 '11 at 19:35
3

You are incorrectly constructing your application/x-www-form-urlencoded document. The simplest way to construct it correctly is to use HTTP::Request::Common's POST either directly

use HTTP::Request::Common qw( POST );
my $request = POST($XMLurl, [ xml_request => $XML ]);
my $response = $ua->request($request);

or indirectly

my $response = $ua->post($XMLurl, [ xml_request => $XML ]);

The body of the request will be

Ws%252BsuHG7Xqk01RaIxm2L/w1L

rather than

Ws%2BsuHG7Xqk01RaIxm2L/w1L

so you'll end up with

Ws%2BsuHG7Xqk01RaIxm2L/w1L

rather than

Ws+suHG7Xqk01RaIxm2L/w1L
ikegami
  • 367,544
  • 15
  • 269
  • 518
  • @daxim, I don't know about it being "the appropriate way". I use it because it's convenient and simple. It's also less prone to misuse than `uri_escape`. – ikegami Mar 23 '11 at 17:32
-1

As a side note, '+' does not require URL encoding so I'm not clear on why you're encoding it in your XML. That aside

I think if you pass HTTP::Request a preformatted string in it's constructor it will not touch the data.

my $ret = HTTP::Request->new('POST', $XMLurl, undef, "xml_request=".$XML); 
vicTROLLA
  • 1,554
  • 12
  • 15