Forum Moderators: coopster & phranque

Message Too Old, No Replies

Consuming a web service using in perl.

Consuming a web service using in perl.

         

frexpopo

9:36 am on Jan 30, 2008 (gmt 0)

10+ Year Member



I am new to perl and in need of some help.
I am integrating an application with (SAP WebAS) my application has some serious problem connecting to the remote service, so I am resorting to scripting to do this. I saw some perl and PHP scripts so decided on perl. I just copied a sample from somewhere and using it. I made few changes but when I run the script I get some errors.
The text in the message is copied from an xml document which the admin of the remote application sent me.

Here is the script in Perl

use strict;
use LWP::UserAgent;
use HTTP::Request::Common;

my $message = '<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema targetNamespace="http://example.com/mm/remedy" xmlns="http://example.com/remedy" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:complexType name="DT_Goods">
<xsd:annotation>
<xsd:appinfo source="http://sap.com/xi/TextID">ae99eb70c4e511dca9d200306e0a30ea
</xsd:appinfo>
</xsd:annotation>
<xsd:sequence>
<xsd:element name="Header" minOccurs="0">
<xsd:annotation>
<xsd:appinfo source="http://sap.com/xi/TextID">734b1b70c41d11dc9e2a00042331c5ab
</xsd:appinfo>
</xsd:annotation>
<xsd:complexType>
<xsd:sequence>
<xsd:element name="PSTNG_DATE" type="xsd:string" minOccurs="0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:annotation>
<xsd:appinfo source="http://sap.com/xi/TextID">734b1b71c41d11dcac6c00042331c5ab
</xsd:appinfo>
</xsd:annotation>
</xsd:element>
<xsd:element name="DOC_DATE" type="xsd:string" minOccurs="0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:annotation>
<xsd:appinfo source="http://sap.com/xi/TextID">734b1b72c41d11dc893900042331c5ab
</xsd:ap#pinfo>
</xsd:annotation>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="Item" maxOccurs="unbounded">
<xsd:annotation>
<xsd:appinfo source="http://sap.com/xi/TextID">734b1b73c41d11dc8f6300042331c5ab
</xsd:appinfo>
</xsd:annotation>
<xsd:complexType>
<xsd:sequence>
<xsd:element name="MATERIAL" type="xsd:string">
<xsd:annotation>
<xsd:appinfo source="http://sap.com/xi/TextID">734b1b74c41d11dc827000042331c5ab
</xsd:appinfo>
</xsd:annotation>
</xsd:element>
<xsd:element name="ENTRY_QNT" type="xsd:string">
<xsd:annotation>
<xsd:appinfo source="http://sap.com/xi/TextID">734b1b75c41d11dca74100042331c5ab
</xsd:appinfo>
</xsd:annotation>
</xsd:element>
<xsd:element name="PLANT" type="xsd:string" minOccurs="0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:annotation>
<xsd:appinfo source="http://sap.com/xi/TextID">734b1b76c41d11dcb19f00042331c5ab
</xsd:appinfo>
</xsd:annotation>
</xsd:element>
<xsd:element name="STGE_LOC" type="xsd:string" minOccurs="0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:annota#tion>
<xsd:appinfo source="http://sap.com/xi/TextID">734b1b77c41d11dc876b00042331c5ab
</xsd:appinfo>
</xsd:annotation>
</xsd:element>
<xsd:element name="COSTCENTER" type="xsd:string" minOccurs="0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:annotation>
<xsd:appinfo source="http://sap.com/xi/TextID">734b1b78c41d11dc994300042331c5ab
</xsd:appinfo>
</xsd:annotation>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>';

my $userAgent = LWP::UserAgent->new();

my $response = $userAgent->request(POST 'http://example.com/sap/xi/adapter_plain? service=BS_REMEDY&namespace=http%3A%2F%2Fexample.com%2Fmm%2Fremedy& interface=MI_GoodsMvt&quos=BE&sap-user=&sap-password=&sap-client=300&sap-language=EN', Content => $message);

print $response->error_as_HTML unless $response->is_success;

print $response->as_string;

Here is the error messages:

<HTML>
<HEAD><TITLE>An Error Occurred</TITLE></HEAD>
<BODY>
<H1>An Error Occurred</H1>
500 Internal Server Error
</BODY>
</HTML>
HTTP/1.1 500 Internal Server Error
Server: SAP Web Application Server (1.0;700)
Content-Length: 493
Content-Type: text/html; charset=utf-8
Client-Date: Wed, 30 Jan 2008 09:08:08 GMT
Client-Peer: 10.31.10.153:8060
Client-Response-Num: 1
Set-Cookie: sap-usercontext=sap-language=EN&sap-client=300; path=/
<SAP:Error><SAP:Category>Application</SAP:Category><SAP:Code>MAPPING.EXCEPTION_DURING_EXECUTE</SAP:Code>
<SAP:P1>com/sap/xi/tf/_MM_GoodsMvt_Request_</SAP:P1><SAP:P2>com.sap.aii.utilxi.misc.api.BaseRuntimeException</SAP:P2>
<SAP:P3>Fatal Error: com.sap.engine.lib.xml.parser.Parser~</SAP:P3><SAP:Stack>com.sap.aii.utilxi.misc.api.
BaseRuntimeException thrown during application mapping com/sap/xi/tf/_MM_GoodsMvt_Request_:
Fatal Error: com.sap.engine.lib.xml.parser.Parser~</SAP:Stack></SAP:Error>

Please let me know ASAP how can this be fixed.

Many thanks in advance

[edited by: jatar_k at 1:05 pm (utc) on Jan. 30, 2008]

[edited by: phranque at 9:21 am (utc) on Jan. 31, 2008]
[edit reason] fixed sidescroll; examplified [/edit]

phranque

11:46 am on Jan 30, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld [webmasterworld.com], frexpopo!

i'm guessing you have your HTTP POST request set up incorrectly.
you probably need to provide a parameter name for the xml content and provide that and the other form parameters either in a hash reference or in the Content: psuedo-header.

maybe more like this:

my $response = $userAgent->request(POST 'http://example.com/sap/xi/adapter_plain',
[service => 'BS_REMEDY',
namespace => 'http%3A%2F%2Fexample.com%2Fmm%2Fremedy',
interface => 'MI_GoodsMvt',
quos => 'BE',
sap-user => '',
sap-password => '',
sap-client => '300',
sap-language => 'EN',
xml-input-param-name => $message]);

[edited by: phranque at 9:13 am (utc) on Jan. 31, 2008]

frexpopo

2:34 pm on Jan 30, 2008 (gmt 0)

10+ Year Member



Hello again,

my $response = $userAgent->request(POST 'http://example.com/sap/xi/adapter_plain',
[ service => 'BS_REMEDY',
namespace => 'http%3A%2F%2Fexample.com%2Fmm%2Fremedy',
interface => 'MI_GoodsMvt_Sync_Out',
quos => 'BE',
'sap-user' => '#*$!x',
'sap-passowrd' => '#*$!x',
'sap-client' => '300',
'sap-language' => 'EN',
'xml-input-param-name' => $message ] );

I have quoted the keys with sap in them and I dont get the error anymore but now I am getting the famous 401 authentication error which I did not get before ...

C:\Work>perl consume.pl
**************
<HTML>
<HEAD><TITLE>An Error Occurred</TITLE></HEAD>
<BODY>
<H1>An Error Occurred</H1>
401 Unauthorized
</BODY>
</HTML>
HTTP/1.1 401 Unauthorized
Server: SAP Web Application Server (1.0;700)
WWW-Authenticate: Basic realm="SAP Web Application Server [DVX]"
Content-Length: 1910
Content-Type: text/html; charset=utf-8
Content-Type: text/html;charset=UTF-8
Client-Date: Wed, 30 Jan 2008 14:27:26 GMT
Client-Peer: 10.31.10.153:8060
Client-Response-Num: 1
Sap-Client: 300
Sap-System: DVX
Set-Cookie: sap-usercontext=sap-language=EN&sap-client=300; path=/
Title: Logon Error Message

I dont get this error when I have the userid and passord included with the rest of the parameters in the URL as in my first initial posting!

Kind Regards
frex

[edited by: phranque at 9:27 am (utc) on Jan. 31, 2008]
[edit reason] examplification [/edit]

frexpopo

2:48 pm on Jan 30, 2008 (gmt 0)

10+ Year Member



Ja Ja Ja!

It was a typo ... spelling problem!

it should be password and not passowrd :-)

frexpopo

2:54 pm on Jan 30, 2008 (gmt 0)

10+ Year Member



things are improving but not sure how much! :)

Now the perl script seem to be connecting but the I get the

following MAPPING.EXCEPTION_DURING_EXECUTE error..

C:\Work>perl consume.pl
<HTML>
<HEAD><TITLE>An Error Occurred</TITLE></HEAD>
<BODY>
<H1>An Error Occurred</H1>
500 Internal Server Error
</BODY>
</HTML>

<SAP:Error><SAP:Category>Application</SAP:Category><SAP:Code>
MAPPING.EXCEPTION_DURING_EXECUTE</SAP:Code><SAP:P1>com/sap/xi/tf/_MM_GoodsMvt_Request_</SAP:P1><SAP:P2>co
m.sap.aii.utilxi.misc.api.BaseRuntimeException</SAP:P2><SAP:P3>
Fatal Error: com.sap.engine.lib.xml.parser.Parser~</SAP:P3><SAP:Stack>com.sap.aii.utilxi.misc.api.BaseR
untimeException thrown during application mapping com/sap/xi/tf/_MM_GoodsMvt_Request_:
Fatal Error: com.sap.engine.lib.xml.parser.Parser~</SAP:Stack></SAP:Error>

phranque

5:05 am on Jan 31, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



maybe it doesn't like the url escaping for the namespace parameter value which looks like it might be necessary for some kind of external mapping description.
namespace => 'http://example.com/mm/remedy',

[edited by: phranque at 9:16 am (utc) on Jan. 31, 2008]

frexpopo

7:55 am on Jan 31, 2008 (gmt 0)

10+ Year Member



So you reckon it could be the foward slash?!
Tried changing it to different format but keep getting the same problem.. I was thinking may the string which is sent in the message which could be the problem!

Perllover

9:51 am on Feb 7, 2008 (gmt 0)

10+ Year Member



Hello Frexpo,

I have had some headache posting payments to Bibit. The problem was that the XML had some wrong cr or lf's due to windows.

I used qq¦ etc but you may also use q¦ etc.

This finnaly worked for me:

my $xml = qq¦
<?xml version='1.0'?>
<!DOCTYPE paymentService PUBLIC '-//Bibit/DTD Bibit PaymentService v1//EN' 'http://dtd.bibit.com/paymentService_v1.dtd'>
<paymentService version='1.4' merchantCode='$merchantCode'>
<submit>
<order orderCode = '$orderCode'>
<description>Uw bestelling:</description>
<amount value='1982' currencyCode = 'EUR' exponent = '2'/>
<orderContent>
<![CDATA[

some html for bibit

]]>
</orderContent>
<paymentMethodMask>
<include code='VISA-SSL'/>
<include code='ECMC-SSL'/>
<include code='AMEX-SSL'/>
</paymentMethodMask>
<shopper>
<shopperEmailAddress>$shopperEmailAddress</shopperEmailAddress> <authenticatedShopperID>$shopperID</authenticatedShopperID>
</shopper>
<shippingAddress>
<address>
<firstName>$firstName</firstName>
<lastName>$lastName</lastName>
<street>$shopperStreet</street>
<postalCode>$postalCode</postalCode>
<city>$shopperCity</city>
<countryCode>$countryCode</countryCode>
<telephoneNumber>$shopperTelephone</telephoneNumber>
</address>
</shippingAddress>
</order>
</submit>
</paymentService>
¦;

The Url I did like this:

my $url = qq¦https://$merchantCode:$password\@secure-test.bibit.com/jsp/merchant/xml/paymentService.jsp¦;

With the \ slash. You may ad some parameters? and & etc

And the posting:

my $ua = LWP::UserAgent->new; ### Make a connection to the Bibit server ###
$ua->timeout(10); ### Little time-out here be nice for the Bibit server ;-) ###
#$ua->protocols_allowed( [ 'http', 'https'] ); ### Turn this on when necessary ###
#$ua->proxy([qw 'https', 'http' )], 'http://www.proxydomain.nl:1111); ### Turn this on when necessary ###
my $response =$ua->post($url, [ 'name' => $xml ]);

Th bibit response is in XML so had to do XML parsing but your response in in html I believe.

Succes,

Greetings, MP

DrDoc

6:58 am on Mar 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Contrary to your belief, that won't actually work. You must (must) use doublequoted attributes in XML.