[oXygen-user] Fwd: [xsl] Hints on http POST

Ben McGinnes ben at adversary.org
Sat Feb 10 13:15:26 CST 2018


On Fri, Feb 02, 2018 at 01:33:15PM -0500, Wendell Piez wrote:
> Hello oXygenistes,
> 
> Over on XSL-List, Martin Honnen kindly pointed me to EXPath as a
> possible answer to my question regarding how to call http POST from
> inside XSLT (see below): he suggests the EXPath http client module.
>
> On 02.02.2018 18:29, Wendell Piez wapiez at wendellpiez.com wrote:
> 
> > I have a need to procure a little bit of XML from a web service
> > running locally. To get it (once) from curl is easy:
> >
> > curl -X POST -d "My string goes in here" localhost:8888/do/me
> >
> > But I need to call it many many times, which I would naturally like to
> > do from inside my transformation.
> >
> > Does anyone have any neat hints of how to do this under unextended (or
> > openly extended) XSLT 3.0?
> 
> However, I find this isn't available in Saxon by default.
> 
> Any ideas of how I can get this to work in oXygen? (or alternative
> strategies?)

Does it really need to be called in the transformation or just
triggered at the same time?

If the latter, then any ant script can call an external command,
including a shell script or other tool.  In which case, pick your
favourite scripting language.

If it needs tighter integration with oXygen, but you don't want to use
Java (fair enough), then any language that can run on the same JVM
will work; Jython, Clojure, etc.

The protestations against my use of a site-wide JDK with oXygen in a
previous thread notwithstanding, that sort of thing does make it a lot
easier to do things like launch Jython in the same JVM as oXygen and
run, for instance, something like this:

Jython 2.7.0 (default:9987c746f838, Apr 29 2015, 02:25:11)
[Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation)] on java1.8.0_162
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> url = "https://www.oxygenxml.com"
>>> r = requests.get(url)
/Library/Java/Jython/jython2.7.0_jdk1.8.0/Lib/site-packages/requests/packages/urllib3/util/ssl_.py:84: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
  warnings.warn(
>>> r.url
u'https://www.oxygenxml.com/'
>>> r.headers
{'date': 'Sat, 10 Feb 2018 18:36:41 GMT', 'server': 'Apache/2.4.18 (Ubuntu)', 'content-length': '13402', 'vary': 'Accept-Encoding', 'content-encoding': 'gzip', 'strict-transport-security': 'max-age=31536000;', 'keep-alive': 'timeout=10, max=500', 'connection': 'Keep-Alive', 'content-type': 'text/html; charset=UTF-8'}
>>> dir(r)
['__attrs__', '__bool__', '__class__', '__delattr__', '__dict__', '__doc__', '__ensure_finalizer__', '__format__', '__getattribute__', '__getstate__', '__hash__', '__init__', '__iter__', '__module__', '__new__', '__nonzero__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__str__', '__subclasshook__', '__weakref__', '_content', '_content_consumed', 'apparent_encoding', 'close', 'connection', 'content', 'cookies', 'elapsed', 'encoding', 'headers', 'history', 'is_permanent_redirect', 'is_redirect', 'iter_content', 'iter_lines', 'json', 'links', 'ok', 'raise_for_status', 'raw', 'reason', 'request', 'status_code', 'text', 'url']
>>> r.status_code
200
>>> r.content.splitlines()[0].replace('><', '>><<').split('><')[18]
'<title>XML Editor</title>'
>>> print(r.content.splitlines()[0].replace('><', '>><<').split('><')[18])
<title>XML Editor</title>
>>>

Ignore the security warning, it's due to Java and the JVM being what
they are.  Other languages might not mention issues like that, but
they're still present.

Oh, yes, it is possible to install Jython into oXygen with a built-in
JRE, but it's a lot more annoying and it increases the application's
install size by however big Jython and all the modules you need to add
really is (at least 100MB or more).  I did it once or twice to prove
it could be done and then left it alone.  The same is likely to be
true of any of the other JVM friendly languages.

Still, given how useful requests is for all web stuff, especially
playing with APIs and how easy it makes POSTs, plus Jython with pip
can install requests straight from PyPI ... the answer to your entire
problem is basically above with just a few slight variations and which
are well documented all over the Net (i.e. requests.post(url,
params=assorted_stuff), where assorted_stuff is a dict containing
whatever you need in there).

You could probably even make it play with oXygen's GUI interface, but
that's *far* more effort than this example needs.


Regards,
Ben
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 228 bytes
Desc: not available
URL: <http://www.oxygenxml.com/pipermail/oxygen-user/attachments/20180211/b47f78a0/attachment.sig>


More information about the oXygen-user mailing list