Wednesday, October 29, 2014

Apache Commons HttpClient for REST in PeopleCode - Part II - More Examples

This is a follow up to my previous post where we discussed how to use Apache Commons HttpClient for REST in PeopleSoft. Here are some more examples.

GetMethod - Adding Cookie (Header) to the Request:

Local JavaObject &jHttp, &jMethod;

/* Initialize HttpClient and set parameters */
&jHttp = CreateJavaObject("org.apache.commons.httpclient.HttpClient");
&jHttp.getHttpConnectionManager().getParams().setConnectionTimeout(20000);

/* Initialize GetMethod */
&sURL = "https://www.test.com/name.v1/get?emplid=12345";
&jMethod = CreateJavaObject("org.apache.commons.httpclient.methods.GetMethod", &sURL);
&jMethod.setFollowRedirects( False);

/* Adding Cookie to the Request */
&jMethod.setRequestHeader("Cookie", "JSESSIONID=64497D7D587637EBF17E128881C04016";
/* Adding Cookie to the Request */

/* Invoke GetMethod */
&return = &jHttp.executeMethod(&jMethod);
&responseStatus = &jMethod.getStatusLine().getStatusCode();

MessageBox(0, "", 0, 0, "Response Status: " | &responseStatus); 
MessageBox(0, "", 0, 0, "Response Message: " | &jMethod.getResponseBodyAsString());

&jMethod.releaseConnection();

PostMethod - Plain Text Content:

Local JavaObject &jHttp, jMethod;

/* Initialize HttpClient and set parameters */

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

/* Initialize PostMethod and Set Request Details */

Local string &sURL = "https://cas.test.com/cas/v1/tickets";
&jMethod = CreateJavaObject("org.apache.commons.httpclient.methods.PostMethod", &sURL);
&jMethod.setFollowRedirects( False);
&jMethod.setRequestHeader("Content-Type", "text/plain");
&jMethod.setRequestBody("username=testuser&password=testpassword");

/* Invoke PostMethod */

&jHttp.executeMethod(&jMethod);

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

Local string &responseBody = &jMethod.getResponseBodyAsString();

MessageBox(0, "", 0, 0, "&responseBody " | &responseBody);

MessageBox(0, "", 0, 0, "&responseStatus " | &responseStatus);

&jMethod.releaseConnection();

PostMethod - Multi-Part Message:

In this example, we will be posting a file on the App Server file system as a multi-part message.

Local JavaObject &jHttp,
&jMethod, &filePart, &partArray, &mPartReqEntity;

/* Initialize HttpClient and set parameters */

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

/* Initialize PostMethod */

Local string &sURL = "https://doc.mgmt.com/upload/v1/filename=test.pdf";
&jMethod = CreateJavaObject("org.apache.commons.httpclient.methods.PostMethod", &sURL);
&jMethod.setFollowRedirects( False);

/* Add Multi-Part Message */


/* Create File Object from App Server */

Local JavaObject &file = CreateJavaObject("java.io.File", "/tmp/test.pdf");

/* Create File Part */
&filePart = CreateJavaObject("org.apache.commons.httpclient.methods.multipart.FilePart", "test.pdf", &file);
/* Add File Part to Part Array */
&partArray = CreateJavaObject("org.apache.commons.httpclient.methods.multipart.Part[]", &filePart);
/* Create Multi-Part Request Entity */
&mPartReqEntity = CreateJavaObject("org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity", &partArray, &jMethod.getParams());
/* Add Multi-Part Request Entity to the Post Method */
&jMethod.setRequestEntity(&mPartReqEntity);

/* Add Multi-Part Message */

/* Invoke PostMethod */
Local integer &return = &jHttp.executeMethod(&jMethod);
Local integer &responseStatus = &jMethod.getStatusLine().getStatusCode();
Local string &responseBody = &jMethod.getResponseBodyAsString();

MessageBox(0, "", 0, 0, "&responseBody " | &responseBody);
MessageBox(0, "", 0, 0, "&responseStatus " | &responseStatus);

&jMethod.releaseConnection();


DeleteMethod:

Local JavaObject &jHttp, jMethod;

/* Initialize HttpClient and set parameters */
&jHttp = CreateJavaObject("org.apache.commons.httpclient.HttpClient");
&jHttp.getHttpConnectionManager().getParams().setConnectionTimeout(20000);

/* Initialize DeleteMethod */
Local string &sURL = "https://doc.mgmt.com/delete/v1/filename=test.pdf";
Local JavaObject &jMethod = CreateJavaObject("org.apache.commons.httpclient.methods.DeleteMethod", &sURL);
&jMethod.setFollowRedirects( False);

/* Invoke DeleteMethod */

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

MessageBox(0, "", 0, 0, "&responseBody " | &responseBody);
MessageBox(0, "", 0, 0, "&responseStatus " | &responseStatus);

&jMethod.releaseConnection();

8 comments:

  1. Hello Sashank,
    Nice article, have gone thru this long time before for reference. Thank you.

    Kind of stuck with consuming a third party service (post) which has a response binary data (file). Not able to get the response properly with &jMethod.getResponse or the &jMethod.getResponseBodyAsString() gets some what close but not getting the full content of the response.
    Any response is highly appreciated.
    Thanks

    ReplyDelete
    Replies
    1. I used getResponseBodyAsString method here for the document upload because the response that I was getting back was plain text.

      If you are getting your response in binary, you could try something like this:
      https://gist.github.com/SasankVemana/872d2f74894da3081ab740ed80bf9b47

      Note: This is just a code snippet so you will need to improvise the code according to your scenario.

      More on Reflection: https://pe0ples0ft.blogspot.com/2014/10/java-reflection-in-peoplecode.html

      Delete
  2. Hi Sasank,
    I was getting an error on the 1st statement
    CreateJavaObject("org.apache.commons.httpclient.HttpClient");
    It says Java Exception: java.lang.NoClassDefFoundError: org/apache/commons/httpclient/HttpClient: finding class org.apache.commons.httpclient.HttpClient (2,725)

    Can you please advise what does this mean?

    Thanks!

    ReplyDelete
    Replies
    1. This means that the class files (jars) are not located on your app server.

      You can find more details on this post (review notes towards the end):
      https://pe0ples0ft.blogspot.com/2014/10/using-apache-commons-httpclient-for.html

      Delete
  3. Hi Sasank,

    Thank you for this reference.. I am in a middle of implementing the same, i am able to connect to the webservice http post.. im sending a pdf file over the webservice but the 3rd party is receiving the pdf with 0bytes and no content. Already added headers content-type application/pdf

    Thanks!

    ReplyDelete
    Replies
    1. Not really sure what is the issue here.

      Where is the pdf file residing? Do you know if you have appropriate permissions to read from that location?

      Delete
  4. Hi Sasank,
    Thanks for all your blog posts, they are very helpful.

    We get the same error "java.lang.NoClassDefFoundError: org/apache/commons/httpclient/HttpClient: finding class org.apache.commons.httpclient.HttpClient (2,725)"

    I verified that all the jar files (httpclient, commons-logging, commons-codec) are in the PSHOME\Class folder on the AppServer. We are at a loss here. Any idea what could be wrong?

    Thanks
    Prasad

    ReplyDelete
    Replies
    1. Hi Sasank,
      We were able to figure out that commons-httpclient.jar is no longer part of PeopleTools. Now they have Httpclient.jar and httpcore.jar with totally different classes and methods.

      Did you have to use these newer modules to do the same? If so can you share that code please?

      Thanks in advance!
      Prasad K

      Delete