[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: http post xml question
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:
> 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
-----------------------------------------------------------------------