Loading ...
Sorry, an error occurred while loading the content.
 

Re: soap headers

Expand Messages
  • jpeyser
    Mike, Things are not always straightforward, but there is always a way to do it in Perl. Sometimes, you have to hack a little. Here is the code to generate the
    Message 1 of 7 , Jun 26, 2003
      Mike,
      Things are not always straightforward, but there is always a way
      to do it in Perl. Sometimes, you have to hack a little. Here is the
      code to generate the SOAP/XML with it's own serializer (that
      redefines a couple of subroutines from SOAP::Serializer).

      Jonathan

      #--------------------------------
      #!/usr/local/bin/perl

      use SOAP::Lite;
      use strict;

      BEGIN {
      package My::Serializer;

      use SOAP::Lite;

      @My::Serializer::ISA = qw(SOAP::Serializer);

      ### Suppress autotyping
      sub autotype {
      my $self = shift;

      return;
      }

      ### Suppress autotyping for arrays
      sub encode_array {
      my($self, $array, $name, $type, $attr) = @_;
      my $items = 'item';

      ### TD: add support for multidimensional, partially
      transmitted and sparse arrays
      my @items = map {$self->encode_object($_, $items)} @$array;
      my $num = @items;
      my($arraytype, %types) = '-';
      for (@items) { $arraytype = $_->[1]->{'xsi:type'} || '-';
      $types{$arraytype}++ }
      $arraytype = sprintf "%s\[$num]", keys %types > 1 ||
      $arraytype eq '-' ? SOAP::Utils::qualify(xsd => $self->xmlschemaclass-
      >anyTypeValue) : $arraytype;

      $type = SOAP::Utils::qualify($self->encprefix => 'Array') if
      $self->autotype && !defined $type;

      return [$name || SOAP::Utils::qualify($self->encprefix
      => 'Array'),
      # {SOAP::Utils::qualify($self->encprefix => 'arrayType') =>
      $arraytype, 'xsi:type' => $self->maptypetouri($type), %$attr},
      {'xsi:type' => $self->maptypetouri($type), %
      $attr},
      [@items],
      $self->gen_id($array)
      ];
      }
      }

      my $username = "username";
      my $password = "password";
      my $account = "account";
      my $originator = "orig";
      my $recipient = "recip";
      my $body = "TEST SOAP";
      my $type = "text";
      my $host = 'www.esendex.com';
      my $proxy = "http://$host/secure/messenger/soap/SendService.asmx";
      my $uri = "com.esendex.ems.soapinterface";

      #The name of the module
      my $soap = SOAP::Lite
      ->uri($uri)
      ->proxy($proxy)
      ->serializer(My::Serializer->new)
      ->envprefix('soap');

      my $MsgFull = [
      SOAP::Data->name(Username => $username),
      SOAP::Data->name(Password => $password),
      SOAP::Data->name(Account => $account),
      ];

      # send the request w/ header
      my $response = $soap->SendMessageFull(

      # HEADER
      SOAP::Header->name(MessengerHeader => $MsgFull)->attr
      ({'xmlns'=> 'com.esendex.ems.soapinterface'}),

      # PARAMETERS FOR BODY
      SOAP::Data->name(originator =>$originator),
      SOAP::Data->name(recipient => $recipient),
      SOAP::Data->name(body => $body),
      SOAP::Data->name(type =>$type),
      SOAP::Data->name(validityperiod => 99)
      );

      print "response = $response\n";

      # get the result and print the error message if it failed
      if (defined (my $result = $response->result)) {
      print "result = $result\n";
      }
      else {
      print "Fault: ", $response->faultstring, "\n",
      "Detail:\n", $response->faultdetail, "\n";
      }




      --- In soaplite@yahoogroups.com, "Mike Solomon" <mike_solomon@l...>
      wrote:
      > Thanks for your help
      >
      > I read the article but I don't think it helped me
      >
      > It probably would if I understood it better
      >
      > I am trying to get the following
      >
      > <soap:Header>
      > <MessengerHeader xmlns="com.esendex.ems.soapinterface">
      > <Username>USERNAME</Username>
      > <Password>PASSWORD</Password>
      > <Account>ACCOUNT</Account>
      > </MessengerHeader>
      > </soap:Header>
      >
      > I have managed to get most of it but can't achieve
      > xmlns="com.esendex.ems.soapinterface
      >
      > I am wondering if I would be better off abandoning soap:lite and
      finding
      > another way of sending the xml
      >
      >
      > Regards
      >
      > Mike Solomon
      >
      > -----Original Message-----
      > From: Randy J. Ray [mailto:rjray@b...]
      > Sent: 26 June 2003 05:15
      > To: Mike
      > Subject: Re: [soaplite] soap headers
      >
      > You are close, but as you noted, the result is three separate
      elements
      > within
      > the Header block, each named for your elements below:
      >
      > > # send the request w/ header
      > > my $response = $soap->SendMessageFull(
      > >
      > > # HEADER
      > > SOAP::Header->name(Username => $username),
      > > SOAP::Header->name(Password => $password),
      > > SOAP::Header->name(Account => $account),
      > >
      > > # PARAMETERS FOR BODY
      > > SOAP::Data->name(originator =>$originator),
      > > SOAP::Data->name(recipient => $recipient),
      > > SOAP::Data->name(body => $body),
      > > SOAP::Data->name(type =>$type),
      > > SOAP::Data->name(validityperiod => 99)
      > >
      > > );
      >
      > What your service is expecting is a single header entity called
      > "MessengerHeader" that itself contains those three elements.
      Something
      > more
      > like:
      >
      > SOAP::Header->name(MessengerHeader => { Username => $username,
      > Password => $password,
      > Account => $account })
      >
      > However, I think that the above will serialize strangely because of
      the
      > defaults applied to non-blessed hash references. See my article
      on .Net
      > programming in Perl:
      >
      > http://www.perl.com/pub/a/2003/03/26/perlanddotnet.html
      >
      > This is very similar to the biggest headache in .Net-- getting
      > serialized
      > structures to match what .Net is expecting. There's a recipe for a
      > generalized
      > structure-encoding approach that should be adaptable to what you're
      > doing.
      >
      > Randy
      > --
    • Randy J. Ray
      ... When you create the SOAP::Header object, you can also call an attr method on it, to specify that is should have an attribute of xmlns whose value is
      Message 2 of 7 , Jun 26, 2003
        On 2003.06.26 08:15, Mike Solomon wrote:

        > I have managed to get most of it but can't achieve
        > xmlns="com.esendex.ems.soapinterface

        When you create the SOAP::Header object, you can also call an "attr" method on
        it, to specify that is should have an attribute of "xmlns" whose value is
        "com.esendex.ems.soapinterface".

        Randy
        --
        """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
        Randy J. Ray Campbell, CA http://www.rjray.org rjray@blackperl.
        com

        Silicon Valley Scale Modelers: http://www.svsm.org
      • Paul Kulchenko
        Mike, use SOAP::Lite maptype = {}; my $messageHeader = SOAP::Header- name(MessageHeader = { Username = SOAP::Data- type( = USERNAME ), Password =
        Message 3 of 7 , Jun 27, 2003
          Mike,

          use SOAP::Lite maptype => {};

          my $messageHeader = SOAP::Header->name(MessageHeader => {
          Username => SOAP::Data->type('' => 'USERNAME'),
          Password => SOAP::Data->type('' => 'PASSWORD'),
          Account => SOAP::Data->type('' => 'ACCOUNT'),
          })->uri("com.esendex.ems.soapinterface")->prefix('');

          Will give you exactly the code you want to see. prefix('') will
          generate "xmlns=" instead of "xmlns:ns1=".

          This example is included in "Programming Web Services with Perl"
          (p284). The documentation will be updated to include that too.

          Best wishes, Paul.

          --- Mike Solomon <mike_solomon@...> wrote:
          > Thanks for your help
          >
          > I read the article but I don't think it helped me
          >
          > It probably would if I understood it better
          >
          > I am trying to get the following
          >
          > <soap:Header>
          > <MessengerHeader xmlns="com.esendex.ems.soapinterface">
          > <Username>USERNAME</Username>
          > <Password>PASSWORD</Password>
          > <Account>ACCOUNT</Account>
          > </MessengerHeader>
          > </soap:Header>
          >
          > I have managed to get most of it but can't achieve
          > xmlns="com.esendex.ems.soapinterface
          >
          > I am wondering if I would be better off abandoning soap:lite and
          > finding
          > another way of sending the xml
          >
          >
          > Regards
          >
          > Mike Solomon
          >
          > -----Original Message-----
          > From: Randy J. Ray [mailto:rjray@...]
          > Sent: 26 June 2003 05:15
          > To: Mike
          > Subject: Re: [soaplite] soap headers
          >
          > You are close, but as you noted, the result is three separate
          > elements
          > within
          > the Header block, each named for your elements below:
          >
          > > # send the request w/ header
          > > my $response = $soap->SendMessageFull(
          > >
          > > # HEADER
          > > SOAP::Header->name(Username => $username),
          > > SOAP::Header->name(Password => $password),
          > > SOAP::Header->name(Account => $account),
          > >
          > > # PARAMETERS FOR BODY
          > > SOAP::Data->name(originator =>$originator),
          > > SOAP::Data->name(recipient => $recipient),
          > > SOAP::Data->name(body => $body),
          > > SOAP::Data->name(type =>$type),
          > > SOAP::Data->name(validityperiod => 99)
          > >
          > > );
          >
          > What your service is expecting is a single header entity called
          > "MessengerHeader" that itself contains those three elements.
          > Something
          > more
          > like:
          >
          > SOAP::Header->name(MessengerHeader => { Username => $username,
          > Password => $password,
          > Account => $account
          > })
          >
          > However, I think that the above will serialize strangely because of
          > the
          > defaults applied to non-blessed hash references. See my article on
          > .Net
          > programming in Perl:
          >
          > http://www.perl.com/pub/a/2003/03/26/perlanddotnet.html
          >
          > This is very similar to the biggest headache in .Net-- getting
          > serialized
          > structures to match what .Net is expecting. There's a recipe for a
          > generalized
          > structure-encoding approach that should be adaptable to what you're
          > doing.
          >
          > Randy
          > --
          >
          >
          >
          > ------------------------ Yahoo! Groups Sponsor
          >
          > To unsubscribe from this group, send an email to:
          > soaplite-unsubscribe@yahoogroups.com
          >
          >
          >
          > Your use of Yahoo! Groups is subject to
          > http://docs.yahoo.com/info/terms/
          >
          >


          __________________________________
          Do you Yahoo!?
          SBC Yahoo! DSL - Now only $29.95 per month!
          http://sbc.yahoo.com
        Your message has been successfully submitted and would be delivered to recipients shortly.