Sending JSON POST query using ABL

Posted by jbetts on 23-Jul-2019 08:33

Hello All,

I am trying to consume the UPS rest API service (https://wwwcie.ups.com/rest/Ship) using OpenEdge, and I believe I am close but coming across issues developing the body of the request.

I would like to send the following JSON query using OpenEdge:

*********************************************************************************************

{
"UPSSecurity":{
"UsernameToken":{
"Username":"username",
"Password":"password"
},
"ServiceAccessToken":{
"AccessLicenseNumber":"accesstoken"
}
},
"ShipmentRequest":{
"Request":{
"RequestOption":"validate",
"TransactionReference":{
"CustomerContext":"Your Customer Context"
}


*********************************************************************************************

 

The current progress code I have is:


*********************************************************************************************

BLOCK-LEVEL ON ERROR UNDO, THROW.

USING OpenEdge.Core.String.
USING OpenEdge.Net.HTTP.ClientBuilder.
USING OpenEdge.Net.HTTP.IHttpRequest.
USING OpenEdge.Net.HTTP.IHttpResponse.
USING OpenEdge.Net.HTTP.RequestBuilder.
USING Progress.Json.ObjectModel.JsonObject.

DEFINE VARIABLE httpUrl AS CHARACTER NO-UNDO.
DEFINE VARIABLE oRequest AS IHttpRequest NO-UNDO.
DEFINE VARIABLE oResponse AS IHttpResponse NO-UNDO.
DEFINE VARIABLE oRequestBody AS string NO-UNDO.
DEFINE VARIABLE oJsonEntity AS JsonObject NO-UNDO.
DEFINE VARIABLE JsonString AS LONGCHAR NO-UNDO.
DEFINE VARIABLE httCust AS HANDLE NO-UNDO.
DEFINE VARIABLE lReturnValue AS LOGICAL NO-UNDO.
DEFINE VARIABLE cTargetType AS CHARACTER NO-UNDO.
DEFINE VARIABLE cFile AS CHARACTER NO-UNDO.
DEFINE VARIABLE lFormatted AS LOGICAL NO-UNDO.

SESSION:DEBUG-ALERT = TRUE.
httpUrl = "wwwcie.ups.com/.../Ship".

oJsonEntity = NEW JsonObject().

oJsonEntity:Add
oJsonEntity:Add("Username", "usernamehere").
oJsonEntity:Add("Password", "passwordhere").

oRequest = RequestBuilder:Post("wwwcie.ups.com/.../Ship", oJsonEntity)
:ContentType('application/json')
:AcceptJson()
:Request.

oResponse = ClientBuilder:Build():Client:Execute(oRequest).


MESSAGE
oResponse:StatusCode SKIP
oResponse:StatusReason SKIP
VIEW-AS ALERT-BOX.

CAST(oResponse:Entity, JsonObject):WriteFile('entity.json',TRUE).

MESSAGE STRING(JsonString)
VIEW-AS ALERT-BOX.

*********************************************************************************************

The problem I am facing is keeping the formatting of the JSON query. Notice that there is a comma between UPSSecurity and ServiceAccessToken.
My question is how do I develop program to send the JSON query in the correct format?

Thanks in advance :)

Kindest Regards,
Joseph

Posted by Peter Judge on 23-Jul-2019 13:34

You'll have to add a bit more code for that JSON structure. If you indent it
{
    "UPSSecurity": {
        "UsernameToken": {
            "Username": "username",
            "Password": "password"
        },
        "ServiceAccessToken": {
            "AccessLicenseNumber": "accesstoken"
        }
    },
    "ShipmentRequest": {
        "Request": {
            "RequestOption": "validate",
            "TransactionReference": {
                "CustomerContext": "Your Customer Context"
            }
        }
    }
}
 
For the username/token part, you'll have to do something like this. I check the contents of my JSON objects when I'm developing them by dumping them to disk
jsonEntity:WriteFile(sessioN:temp-dir + 'data.json', yes).    // the yes is to pretty-print the data
 
jsonEntity = new JsonObject().
jsonData = new JsonObject().
jsonEntity:Add('UPSSecurity', jsonData).
     jsonDetail = new JsonObject().
     jsonData:Add('UsernameToken', jsonDetail).
                jsonDetail:Add('Username', 'username').
               jsonDefailt:Add('Password', 'password').
               
     jsonDetail = new JsonObject().
     jsonData:Add('ServiceAccessToken', jsonDetail).
                jsonDetail:Add('AccessLicenstNumber', 'accesstoken').
                               
jsonData = new JsonObect().
jsonEntity:Add('ShipmentRequest, jsonData).
                // etc
 
 
 
 

All Replies

Posted by Peter Judge on 23-Jul-2019 13:34

You'll have to add a bit more code for that JSON structure. If you indent it
{
    "UPSSecurity": {
        "UsernameToken": {
            "Username": "username",
            "Password": "password"
        },
        "ServiceAccessToken": {
            "AccessLicenseNumber": "accesstoken"
        }
    },
    "ShipmentRequest": {
        "Request": {
            "RequestOption": "validate",
            "TransactionReference": {
                "CustomerContext": "Your Customer Context"
            }
        }
    }
}
 
For the username/token part, you'll have to do something like this. I check the contents of my JSON objects when I'm developing them by dumping them to disk
jsonEntity:WriteFile(sessioN:temp-dir + 'data.json', yes).    // the yes is to pretty-print the data
 
jsonEntity = new JsonObject().
jsonData = new JsonObject().
jsonEntity:Add('UPSSecurity', jsonData).
     jsonDetail = new JsonObject().
     jsonData:Add('UsernameToken', jsonDetail).
                jsonDetail:Add('Username', 'username').
               jsonDefailt:Add('Password', 'password').
               
     jsonDetail = new JsonObject().
     jsonData:Add('ServiceAccessToken', jsonDetail).
                jsonDetail:Add('AccessLicenstNumber', 'accesstoken').
                               
jsonData = new JsonObect().
jsonEntity:Add('ShipmentRequest, jsonData).
                // etc
 
 
 
 

Posted by jbetts on 23-Jul-2019 14:34

Perfect Peter, thanks a million.

It's a strange JSON structure that UPS require, but this has resolved my issue nicely.

Kindest Regards,

Joseph

This thread is closed