Salesforce

How to send and receive a simple JSON document with the ABL HTTP Client

« Go Back

Information

 
TitleHow to send and receive a simple JSON document with the ABL HTTP Client
URL NameHow-to-send-and-receive-a-simple-JSON-document-with-the-ABL-HTTP-Client
Article Number000205096
EnvironmentProduct: OpenEdge
Version: 11.x , 12.x
OS: All supported platforms
Other: HTTP Client
Question/Problem Description
How to send and receive a simple JSON document with the ABL HTTP Client.
Sample ABL code to send and receive a simple JSON document with the HTTP Client.
Steps to Reproduce
Clarifying Information
Error Message
Defect Number
Enhancement Number
Cause
Resolution
Use ABL Objects and Arrays to create and process the JSON document within the HTTP request and response. The use of the ABL Object is illustrated through the following sample code:
 
BLOCK-LEVEL ON ERROR UNDO, THROW.

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

/* *************************** Definitions ************************** */
DEFINE VARIABLE oClient AS IHttpClient NO-UNDO.
DEFINE VARIABLE oRequest AS IHttpRequest NO-UNDO.
DEFINE VARIABLE oResponse AS IHttpResponse NO-UNDO.
DEFINE VARIABLE oPayload AS JsonObject NO-UNDO.
DEFINE VARIABLE oJsonResp AS JsonObject NO-UNDO.
DEFINE VARIABLE oJson AS JsonObject NO-UNDO.
DEFINE VARIABLE value1 AS DECIMAL NO-UNDO.
DEFINE VARIABLE valuE2 AS CHARACTER NO-UNDO.

/* *************************** Main Block *************************** */

/* Dump request and response */
/* See https://knowledgebase.progress.com/articles/Knowledge/how-to-log-raw-http-messages-with-httpclient */
// LOG-MANAGER:LOGFILE-NAME="client-log.txt".
// LOG-MANAGER:LOGGING-LEVEL=5.

oClient = ClientBuilder:Build():Client.

/* Create payload for HTTP request */
oPayload = NEW JsonObject().
oPayload:ADD('value1',47.11).
oPayload:ADD('value2','finished').

oRequest = RequestBuilder:Post('http://httpbin.org/anything', oPayload)
:ContentType('application/json')
:Request.

oResponse = oClient:Execute(oRequest).

MESSAGE oResponse:StatusCode
VIEW-AS ALERT-BOX.

/* Check if response is a JSON object */
IF TYPE-OF(oResponse:Entity, JsonObject) THEN
ASSIGN oJsonResp = CAST(oResponse:Entity, JsonObject).

/* Retrieve the JSON key-value pairs located in the 'json' label */
oJson = oJsonResp:GetJsonObject('json').

value1 = oJson:GetDecimal('value1').
value2 = oJson:GetJsonText('value2').

MESSAGE "value1: " + STRING(value1) SKIP
"value2: " + value2
VIEW-AS ALERT-BOX INFORMATION BUTTONS OK.

CATCH oError AS Progress.Lang.Error :
MESSAGE oError:GetMessage(1) SKIP(2)
oError:CallStack
VIEW-AS ALERT-BOX.
END CATCH.

This sample code will send the following HTTP request:
POST /anything HTTP/1.1
User-Agent: OpenEdge-HttpClient/0.4.0 (WIN32/64) OpenEdge/11.7.10.0.2067 Lib-ABLSockets/0.5.0
Host: httpbin.org
Content-Type: application/json
Content-Length: 36
Accept: */*

{"value1":47.11,"value2":"finished"}

The following HTTP response is returned:
HTTP/1.1 200 OK
Date: Mon, 06 Sep 2021 12:07:34 GMT
Content-Type: application/json
Content-Length: 572
Connection: keep-alive
Server: gunicorn/19.9.0
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true

{
  "args": {}, 
  "data": "{\"value1\":47.11,\"value2\":\"finished\"}", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept": "*/*", 
    "Content-Length": "36", 
    "Content-Type": "application/json", 
    "Host": "httpbin.org", 
    "User-Agent": "OpenEdge-HttpClient/0.4.0 (WIN32/64) OpenEdge/11.7.10.0.2067 Lib-ABLSockets/0.5.0", 
    "X-Amzn-Trace-Id": "Root=1-61360486-027f2a7e4bfc63f35bd467a2"
  }, 
  "json": {
    "value1": 47.11, 
    "value2": "finished"
  }, 
  "method": "POST", 
  "origin": "12.345.67.89", 
  "url": "http://httpbin.org/anything"
}

 
Workaround
Notes
References to Other Documentation:

Develop with JSON in OpenEdge
https://docs.progress.com/bundle/openedge-abl-use-json/page/About-JSON.html

Progress Article(s):
How to create JSON using JsonObject and JsonArray objects?
Is there an HTTP client for OpenEdge?
Keyword Phrase
Last Modified Date2/10/2023 9:32 AM

Powered by