Tuesday, April 21, 2020

Creating JSON request for REST web services

PeopleSoft provides Document technology to be used to generate JSON request messages but in my experience they are very restrictive especially when working on integrating with 3rd party web services. So following is what I did to generate a JSON request message to post to a 3rd party REST web service. 

The request that I have to generate is in the following form.
[
   {
      "attrib1":"value1",
      "attrib2":"value2",
      "attrib3":{
         "attrib3_1":"values3_1",
         "attrib3_2":"values3_2",
         "attrib3_3":"values3_3",
         "attrib3_4":"values3_4"
      }
   }
]

I am running PT 8.57.x and at this time its not possible to build a document with the root node as an array as shown in the example below. Also I have nested compounds which is also a challenge, the parent compound does not have a label where as the child does. 
So to build something like above I am using the CreateJsonBuilder API provided by PeopleSoft.

Local JsonBuilder &jbldr = CreateJsonBuilder();
Local JsonArray &jArray;
Local string &json;
Local message &request, &response;
Local boolean &bRet;

&jbldr.StartArray(""); /* no label */
 &jbldr.StartObject(""); /* no label */
  &jbldr.AddProperty("attrib1", "value1");
  &jbldr.AddProperty("attrib2", "value2");
   &jbldr.StartObject("attrib3"); /* need a label */
    &jbldr.AddProperty("attrib3_1", "value3_1");
    &jbldr.AddProperty("attrib3_2", "value3_2");
    &jbldr.AddProperty("attrib3_3", "value3_3");
    &jbldr.AddProperty("attrib3_4", "value3_4");
   &jbldr.EndObject("attrib3"); /* closing out the compound or JSONObject */
 &jbldr.EndObject("");
&jbldr.EndArray("");


/* this will return the array just like what I want */
&jArray = &jbldr.GetRootNode().GetJsonObject().GetJsonArray("");
&json = &jArray.ToString();

Created a basic non-rowset based message and assigned that as the request message in my service operation. Use this method to set the content for the message segment for a non-rowset-based message only.

&bRet = &request.SetContentString(&json);
&response = %IntBroker.SyncRequest(&request);

That's it, works like a charm.

6 comments:

  1. Hi Deepak, Great code.
    I wonder if I am writing a peoplesoft REST web service for a 3rd party to consume. This web service provide a Voucher File attachment from peoplesoft to 3rd party. the input parm is voucher ID, then what would the response document for REST service would look like, since output is a .TXT file, or .JPG file etc. Kindly advise. DS.

    ReplyDelete
    Replies
    1. D.Suneja, Unfortunately I haven't had the opportunity to try this out yet. I have done it via SOAP messages leveraging MTOM. You can review my post https://psdips.blogspot.com/2016/03/sending-and-receiving-mtom-encoded.html and see if that gives you any clues.
      Thanks, DS.

      Delete
  2. Hi Deepak,
    I am working on a HTTP PATCH method in REST. For URI - using a document technology. But for REQUEST message trying to use a Non-Rowset message with createJSONbuilder. I am getting this error "This method can only be used by nonrowset-based messages. (2,852) "


    &PatchSOrequest = CreateMessage(Operation.serviceoperation_of_PATCH);

    &uriDocument = &PatchSOrequest.GetURIDocument().DocumentElement;
    &uriDocument.GetPropertyByName("parm1").Value = "emp";
    &uriDocument.GetPropertyByName("parm2").Value = &ID;
    &PatchSOrequest.URIResourceIndex = 1;

    &request = CreateMessage(Operation.serviceoperation_of_PATCH, %IntBroker_Request);

    /* building the json here*/

    &json = &jsonbldr.ToString();

    Local boolean &boolean = &PatchSOrequest.SetContentString(&json);
    /* Local boolean &boolean = &request.SetContentString(&json); - tried this code as well */



    &response = %IntBroker.SyncRequest(&PatchSOrequest);

    Unable to find a way out for this. Suggestion from your expertise would be helpful. Thanks!

    ReplyDelete
    Replies
    1. Not sure if I can debug this without knowing all the details - you do not need the following line.

      &request = CreateMessage(Operation.serviceoperation_of_PATCH, %IntBroker_Request);

      At what point are you getting the error? Are you getting it for method SetContentString? If yes then the message type of your request message is not defined as nonrowset. So please verify and let me know.

      Delete
  3. This comment has been removed by the author.

    ReplyDelete

  4. Hi Deepak, Do you happen to try any code to submit synchronous messages on adhoc basis. like say if any synchrounous message failed due to any reason and we are buolding a custom page to submit the message again. How do we achieve this?

    ReplyDelete