Using Apache HttpClient from PeopleCode

By Chris Malek | Tue, Aug 12, 2014

I have been struggling calling some REST based services from PeopleSoft. Specifically where PeopleSoft is the client. In a previous KB article, I wrote about how the Integration Broker was removing response data when acting as the client for HTTP Status codes that were not “success”.

Struggling through those issues got me thinking about alternatives as I could not wait a year for them to be fixed and installed in a client’s production database. Luckily, I remembered stumbling across an article on GreyHeller’s blog many years ago about using the Apache HTTPClient from PeopleCode. It seems the PeopleCode example is no longer showing on that 2006 article but I was able to work through it.

Additionally, the appropriate JAR files were already in place in the environment I was testing this in. It looks like PeopleTools is delivering these JAR files as part of a standard install now as far as I can tell. Some Google searches revealed that the Environment Management Framework (EMF) might be using this which would explain why it was already loaded. UPDATE: Jim Marion reached out to me and pointed out that his latest VM’s did not have the jar files. So you will likely have to install these from the Jakarta Site. I think I got lucky and the client had them installed for some other functionality.

Below you will find several PeopleCode snippets on how to use the Apache HttpClient to do various HTTP methods.

  • This bypasses the integration broker entirely.
  • There will be no sort logging in the integration broker
  • You need to make sure that the application server has connectively to the target URLS.
  • Make sure your URLs are properly URLEncoded.

Using this Apache HTTPClient solution was orders of magnitude more simple for calling REST based services that using the integration broker. It was also much less “brittle” which will be a topic covered in another post about PeopleTools REST services based on my struggles with them that is still in draft on my computer.

Note: These examples have zero error handling for simplicity of this post only.

HTTP Get Example

Here is a simple GET Method to Google.

Local JavaObject &jHttp = CreateJavaObject("org.apache.commons.httpclient.HttpClient");
&jHttp.getHttpConnectionManager().getParams().setConnectionTimeout(20000);

Local string &sURL = "http://www.google.com";

Local JavaObject &jMethod = CreateJavaObject("org.apache.commons.httpclient.methods.GetMethod", &sURL);
&jMethod.setFollowRedirects( False);

 &jHttp.executeMethod(&jMethod);

local string &responseBody = &jMethod.getResponseBodyAsString();
local integer &responseStatus = &jMethod.getStatusLine().getStatusCode();

HTTP Post of JSON

Here is a simple example of an HTTP Post of some JSON data. If you need to post text or XML just changed the Content-Type header.

Local JavaObject &jHttp = CreateJavaObject("org.apache.commons.httpclient.HttpClient");
&jHttp.getHttpConnectionManager().getParams().setConnectionTimeout(20000);

Local string &sURL = "https://www.some-super-cool-webservice.com/api/v1/users";

Local JavaObject &jMethod = CreateJavaObject("org.apache.commons.httpclient.methods.PostMethod", &sURL);

&jMethod.setFollowRedirects( False);

&jMethod.setRequestHeader("Content-Type", "application/json");

Local string &jsonBody;

/* get some abritrary JSON from HTML Area in this case because it is easy */
/* JSON in PeopleTools is painful */

&jsonBody = GetHTMLText(HTML.NU_OKTA_UNIT_TEST_JSON);
&jMethod.setRequestBody(&jsonBody);

local string &responseBody = &jMethod.getResponseBodyAsString();
local integer &responseStatus = &jMethod.getStatusLine().getStatusCode();

HTTP Put

If you need to do an HTTP Put Method then just use the POST example above and change one line.

Local JavaObject &jMethod = CreateJavaObject("org.apache.commons.httpclient.methods.PutMethod", &sURL);

HTTP Delete

If you need to do an HTTP Delete Method then just use the POST example above and change one line.

Local JavaObject &jMethod = CreateJavaObject("org.apache.commons.httpclient.methods.DeleteMethod", &sURL);

A Delete often does not have a request body so you can also leave off the call to setRequestBody.

HTTP Form Post

Here is an example of an HTTP Form Post.

Local JavaObject  &jHttp = CreateJavaObject("org.apache.commons.httpclient.HttpClient");
&jHttp.getHttpConnectionManager().getParams().setConnectionTimeout(10000);
&sURL = "http://some-host.com";


Local JavaObject &jMethod = CreateJavaObject("org.apache.commons.httpclient.methods.PostMethod", &sURL);
rem &jMethod.setFollowRedirects( False);

&jMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");


Local string &formData;
&formData = "formfield1=" | EncodeURLForQueryString("value1");
&formData = &formData | "&" | "formfield2=" | EncodeURLForQueryString("value2");

&jMethod.setRequestBody(&formData);

&jHttp.executeMethod(&jMethod);

Local string &responseBody = &jMethod.getResponseBodyAsString();
Local integer &responseStatus = &jMethod.getStatusLine().getStatusCode();

HTTP Post of JSON with BASIC Auth

Here is a simple example of an HTTP Post of some JSON data. If you need to post text or XML just changed the Content-Type header.

Additionally, in this example, we are adding a Basic Auth Header.

Local JavaObject &jHttp = CreateJavaObject("org.apache.commons.httpclient.HttpClient");
&jHttp.getHttpConnectionManager().getParams().setConnectionTimeout(20000);

Local string &sURL = "https://www.some-super-cool-webservice.com/api/v1/users";

Local JavaObject &jMethod = CreateJavaObject("org.apache.commons.httpclient.methods.PostMethod", &sURL);

&jMethod.setFollowRedirects( False);

&jMethod.setRequestHeader("Content-Type", "application/json");

/* Base 64 encode Basic Auth String. 
Local object &crypto = CreateObject("Crypt");
&crypto.Open("BASE64_ENCRYPT");
&crypto.UpdateData("clear-text-basic-auth-key");  /* replace with your secret key */
Local string &Base64AuthHeader = &crypto.Result;
&Base64AuthHeader = Clean(&Base64AuthHeader); /* CRYPTO CLASS ADDED CARRIAGE RETURN AT END, MUST REMOVE */

&jMethod.setRequestHeader("Authorization", "Basic " | &Base64AuthHeader);




Local string &jsonBody;

/* get some abritrary JSON from HTML Area in this case because it is easy */
/* JSON in PeopleTools is painful */

&jsonBody = GetHTMLText(HTML.NU_OKTA_UNIT_TEST_JSON);
&jMethod.setRequestBody(&jsonBody);

local string &responseBody = &jMethod.getResponseBodyAsString();
local integer &responseStatus = &jMethod.getStatusLine().getStatusCode();

HTTP Get of a Binary File

Special thanks goes to Venu for providing this one.

This example shows how to “GET” a binary file from another server. This file could be an image, PDF, etc. In this case, we save it to a local file on the application server. Once it is on the application server, you will likely need some additional PeopleCode to push it to an attachment table or FTP store depending on your needs.

Local JavaObject &jHttp = CreateJavaObject("org.apache.commons.httpclient.HttpClient");
&jHttp.getHttpConnectionManager().getParams().setConnectionTimeout(20000);

Local JavaObject &jMethod = CreateJavaObject("org.apache.commons.httpclient.methods.GetMethod", &sURL);
&jMethod.setFollowRedirects( False);



/* Get the response in stream */
&responseBody = &jMethod.getResponseBodyAsStream();

/* Convert Input Stream to Byte Array */

&ioUtils = GetJavaClass("org.apache.commons.io.IOUtils");

&byteArray = &ioUtils.toByteArray(&responseBody);

/* Open an File output Stream on app server directory */

Local JavaObject &out = CreateJavaObject("java.io.FileOutputStream", &TargetFullPath);

/*Write the response to output file */

&out.write(&byteArray);

Get HTTP Response Headers

From the examples above, if you wanted to pull out a response header you could do something like the following. Please note that the value of @customValue will probably have the header key and value so you will have to split it out by the : seperator.


Local string  &customHeader;
&customHeader = &jMethod.getResponseHeader("x-custom").toString();

Let me know if you find these useful.

Author Info
Chris Malek

Chris Malek is a PeopleTools® Technical Consultant with two decades of experience working on PeopleSoft enterprise software projects. He is available for consulting engagements.

About Chris Work with Chris
Looking for pain-free PeopleSoft web services? 😀
PeopleSoft Simple Web Services (SWS)

Introducing a small but powerful PeopleSoft bolt-on that makes web services very easy. If you have a SQL statement, you can turn that into a web service in PeopleSoft in a few minutes.

Book
Integration Broker - The Missing Manual

I am in the process of writing a book called "Integration Broker - The Missing Manual" that you can read online.