[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: http post xml question



Hi Dusty,

Either use global variables (some would consider that sloppy -- but 
it'll work) or else pass a data structure to HTTPAPI.  instead of 
%addr(rate), use %addr(someds).  Then that DS would be the first parm to 
your "Incoming" routine instead of "rate".




Dusty Edenfield wrote:
>    I got my original code to work using http_url_post_xml.  The problem
>    was with my content type.  I had to change it from 'text/xml' to
>    'application/x-www-form-urlencode'.  Then it worked fine.
>    I now need to know the best way to parse multiple elements from the
>    xml response into my program.  In the following code I pass rate to
>    Incoming and then I move the information I need into rate and I'm able
>    to display rate on the screen.  How do I bring back multiple values
>    that I've parsed from the xml response.  I tried to do it with the
>    variable rtime below, but I don't get the xml response I'm expecting.
>    The elements I want to parse just don't show up in the response I get
>    back.  I commented out the rtime code below.
>     rc = http_url_post_xml(
>                      '[1]http://xxxx/xxx'
>                     +'xxxAction.do?'
>                             : %addr(TREE) + 2
>                             : %len(TREE)
>                             : *NULL
>                             : %paddr(Incoming)
>                             : %addr(rate)
>                             : HTTP_TIMEOUT
>                             : HTTP_USERAGENT
>                             : 'application/x-www-form-urlencoded'
>                             : *blanks );
>           http_debug(*OFF);
>           if (rc <> 1);
>              http_crash();
>           else;
>              Result = rate;
>           // Result = 'Line Item is '+ rate +' Response is'+ rtime;
>              dsply Result;
>              http_comp(%trim(Result));
>           endif;
>           *inlr = *on;
>          /end-free
>         P Incoming        B
>         D Incoming        PI
>         D   rate                         8A
>         D*  rtime                        8A
>         D   depth                       10I 0 value
>         D   name                      1024A   varying const
>         D   path                     24576A   varying const
>         D   value                    65535A   varying const
>         D   attrs                         *   dim(32767)
>         D                                     const options(*varsize)
>         D atof            PR             8A   extproc('atof')
>         D   string                        *   value options(*string)
>          /free
>              if (name = 'lineItemId');
>                 rate = value;
>              endif;
>           // if (name = 'responseTime');
>           //    rtime = value;
>           // endif;
>          /end-free
>         P                 E
> 
>    On Wed, Feb 25, 2009 at 7:08 PM, Scott Klement
>    <[2]sk@xxxxxxxxxxxxxxxx> wrote:
> 
>      hi Dusty,
>      Are you suggesting that you can simply click the link in a browser
>      and
>      it'll work...?  No HTML forms, no JavaScript, etc?
>      if so, then you should be able to do the same thing in HTTPAPI as
>      follows:
>          eval URL = 'http://IP:PORT/....etc... put entire XML here...';
>          rc = http_url_get(URL: '/tmp/test.txt');
>      That's it... it sends the URL to the server and puts the response
>      in
>      /tmp/test.txt.
>      I think maybe you're making it more complicated than it needs to be
>      because you're copying EXAMPLE18, which is for a completely
>      different
>      type of web service (EXAMPLE18 is for a SOAP service)
>      HOWEVER...  now is where it starts to get less simple!  That'll
>      only
>      work if the XML document is made up purely of characters that are
>      legal
>      in a URL.  (no, the fact that it worked in a browser does NOT mean
>      that
>      it's all legal chars in a URL -- browsers have extra code to fix
>      common
>      mistakes that people make -- whereas HTTPAPI doesn't.)
>      HTTPAPI does have routines to help make your data valid for putting
>      in a
>      URL -- it's called a "URL encoder", because it encodes your data so
>      that
>      it's legal to put into a URL.
>      so you'd do this:
>            enc = http_url_encoder_new();  // make a new encoder
>            http_url_encoder_addvar( enc: 'input': 'XML DATA HERE');
>            URL = 'http://IP:PORT/Action.do?' +
>      http_url_encoder_getstr(enc);
>      This creates an encoder, adds a new variable to it (the input
>      variable,
>      which is set to the XML document) then extracts the encoded data as
>      a
>      string and adds it to the URL.
>      then you'd do this with the URL you just built:
>            rc = http_url_get(URL:'/tmp/test.txt');
>      If you want HTTPAPI to try to parse the XML you get back as a
>      response
>      (HTTPAPI will never try to parse the outgoing stuff, only the
>      incoming
>      stuff) then call http_url_get_xml() instead of http_url_get().
>      That's
>      where you'd create an "Incoming" procedure (you can name the
>      procedure
>      anything you like -- but Incoming was what I used in my examples.)
>      Here's a more detailed example -- please note that this is off the
>      top
>      of my head.  I've made no attempt to compile or test this (really,
>      I
>      can't test it, since I don't have access to the web service itself)
>           H DFTACTGRP(*NO) BNDDIR('HTTPAPI')
>            /copy HTTPAPI_H
>           D enc             s                   like(HTTP_URL_ENCODER)
>           D rc              s             10i 0
>           D URL             S          32767a   varying
>           D TREE            S          32767a   varying
>            /free
>              TREE = '<Request>'
>                   + '<transactionId>123456</transactionId>'
>                   + '<lineItems>'
>                   + '<LineItem>'
>                   + '<lineItemId>123xyz</lineItemId>'
>                   + '</LineItem>'
>                   + '</lineItems>'
>                   + '</Request>';
>              http_debug(*ON);
>              // ----------------------------------------------
>              //  Since the XML needs to be put into a URL,
>              //  it needs to be URL encoded.  If not, the URL
>              //  will be invalid, and won't work with HTTP...
>              //
>              //  The steps for URL encoding are:
>              //
>              //   a) Create a new URL encoder (this reserves
>              //       memory to store the encoded data in.)
>              //   b) Add the "input" variable, and assign the
>              //       XML document to it.
>              //   c) You can add more than one variable if you
>              //       need to (though I'm not in this example)
>              //   d) construct the URL from the string stored
>              //       in the encoder.
>              //   e) Free up the memory used by the encoder
>              // ----------------------------------------------
>              enc = http_url_encoder_new();
>              http_url_encoder_addvar( enc
>                                     : 'input'
>                                     : %addr(TREE)+2
>                                     : %len(TREE) );
>              URL = 'http://IP:PORT/Action.do?'
>                  + http_url_encoder_getstr(enc);
>              http_url_encoder_free(enc);
>              // ----------------------------------------------
>              //   Now that we've build a URL containing
>              //   XML data, make a GET request to that URL
>              //
>              //   The result will be saved to an IFS file
>              //   named /tmp/dusty_test.xml
>              //
>              //   You could then use XML-INTO or whatever
>              //   you like to parse the response (if it's XML)
>              // ----------------------------------------------
>              rc = http_url_get( URL: '/tmp/dusty_test.xml');
>              if (rc <> 1);
>                 http_crash();
>              endif;
>              // ----------------------------------------------
>              // If you want HTTPAPI to help with XML parsing
>              //  (instead of XML-INTO or another method)
>              // you'll call http_url_get_xml().
>              //
>              // NOTE: this is an ALTERNATIVE to the previous
>              //       step (don't do both)
>              // ----------------------------------------------
>              rc = http_url_get_xml( URL
>                                   : *NULL
>                                   : %paddr(Incoming)
>                                   : %addr(rate) );
>              if (rc <> 1);
>                 http_crash();
>              endif;
>              *inlr = *on;
>            /end-free
>            ... code for your Incoming Procedure would be here...
>            ...  just like you coded before ...
> 
>    Dusty Edenfield wrote:
>    >    I need to use HTTPAPI to interact with a web service.  I've got a
>    url
>    >    that I need to hit with some xml behind it.  I'm having to do
>    this in
>    >    order to interact with 3rd party software from our ERP
>    applications.
>    >    They've given me a url that they said would work if I click on it
>    and
>    >    it does.  Once I launched the URL I had to view source to see the
>    >    actual response, but regardless, I've verified that it works.
>    >    The format of the url is something like this
>    >
>    http://ip:port/Action.do?<?xml%20version='1.0'%20encoding='UTF-8'?>&in
>    >
>    put=<mainElement><stuff1>ABC</stuff1><stuff2>123</stuff2></mainElement
>    >    >
>    >    What I need to do is do a post of that url and xml and then
>    receive
>    >    the response back, which will be in xml, and parse it in to
>    fields in
>    >    the RPG program.  What is the best way to do it? Should I use
>    >    http_url_post_xml or http_url_post_stmf_xml or
>    http_url_post_stmf?
>    >    It's all kinda confusing.  Especially when I have a pretty good
>    idea
>    >    of what I want to do.  I just don't know which tools to use to do
>    it.
>    >    I just want to mimic the action of the example url that was given
>    to
>    >    me.
>    >    I'm attaching the code I've been using so far without any luck.
>    I can
>    >    hit the 3rd party server, but they just see an empty string where
>    my
>    >    xml is supposed to be.
>    >    Thanks for any help.  Example code would be awesome if anyone
>    knows
>    >    how to fix this.
>    >
>    >
>    >
> 
>      >
>      -------------------------------------------------------------------
>      -----
> 
>    >
>    >
>    ----------------------------------------------------------------------
>    -
>    > This is the FTPAPI mailing list.  To unsubscribe, please go to:
>    > [3]http://www.scottklement.com/mailman/listinfo/ftpapi
>    >
>    ----------------------------------------------------------------------
>    -
>    ----------------------------------------------------------------------
>    -
>    This is the FTPAPI mailing list.  To unsubscribe, please go to:
>    [4]http://www.scottklement.com/mailman/listinfo/ftpapi
>    ----------------------------------------------------------------------
>    -
> 
> References
> 
>    1. http://xxxx/xxx
>    2. mailto:sk@xxxxxxxxxxxxxxxx
>    3. http://www.scottklement.com/mailman/listinfo/ftpapi
>    4. http://www.scottklement.com/mailman/listinfo/ftpapi
> 
> 
> 
> ------------------------------------------------------------------------
> 
> -----------------------------------------------------------------------
> This is the FTPAPI mailing list.  To unsubscribe, please go to:
> http://www.scottklement.com/mailman/listinfo/ftpapi
> -----------------------------------------------------------------------

-----------------------------------------------------------------------
This is the FTPAPI mailing list.  To unsubscribe, please go to:
http://www.scottklement.com/mailman/listinfo/ftpapi
-----------------------------------------------------------------------