Hi,
for the first time I have to deal with REST JSON.
In my code below, I need to POST the following JSON file :
data:{
customers:[
{
id_accounting:1,
id_totalgest:2,
error:false,
message:""
},
{
id_accounting:3,
id_totalgest:4,
error:true,
message:"Some reason ..."
}],
products:[
{
id_accounting:5,
id_totalgest:6,
error:false,
message:""
}],
services:[
{
id_accounting:7,
id_totalgest:0,
error:true,
message:"Some reason ..."
}]
}
}
Below the code. I don't understand how I can specify JSON file. I found the code in same OE example. I'm using OE 11.7.
Any help is very appreciate. Many thanks.
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 oRequestBody AS JsonObject NO-UNDO.
DEFINE VARIABLE oJsonEntity AS JsonObject NO-UNDO.
DEFINE VARIABLE JsonString AS LONGCHAR NO-UNDO.
DEFINE VARIABLE jsonReq AS NO-UNDO.
SESSION:DEBUG-ALERT = TRUE.
httpUrl = "www.googleapis.com/.../token".
oRequestBody = NEW JsonObject().
//jsonReq ='~{"<some JSON if required for the request>"~}'.
oRequestBody = NEW String(jsonReq). ??????????
/* oRequestBody = NEW String('{
data:{
customers:[
{
id_accounting:1,
id_totalgest:2,
error:false,
message:""
},
{
id_accounting:3,
id_totalgest:4,
error:true,
message:"Some reason ..."
}],
products:[
{
id_accounting:5,
id_totalgest:6,
error:false,
message:""
}],
services:[
{
id_accounting:7,
id_totalgest:0,
error:true,
message:"Some reason ..."
}]
}
}
'). */
//oRequestBody = new JsonObject().
//oRequestBody:Add("CustNum", "200").
//oRequestBody:Add("Name", "Excellent Sports Apparel").
oRequest = RequestBuilder:Post(httpURL, oRequestBody)
:ContentType('application/json')
:AcceptJson()
:Request.
oResponse = ClientBuilder:Build():Client:Execute(oRequest).
MESSAGE
oResponse:StatusCode SKIP
oResponse:StatusReason SKIP
oResponse:ContentType skip
oResponse:ContentLength skip
vIEW-AS ALERT-BOX.
oJsonEntity = CAST(oResponse:Entity, JsonObject).
oJsonEntity:Write(JsonString, TRUE).
MESSAGE STRING(JsonString)
VIEW-AS ALERT-BOX.
Hello Giancarlo,
> Below the code. I don't understand how I can specify JSON file. I found the code in same OE example. I'm using OE 11.7.
It looks like you have tried some few things with the sample program from the Knowledge Base:
- [View:https://knowledgebase.progress.com/articles/Article/How-to-send-a-string-as-the-Request-Body-when-using-the-OpenEdge-HTTP-client:550:50]
I have updated your code to make it work using 3 different approaches.
However, could you be more specific on your use case and what was the exact issue your were running into?
Is your issue with the datatype of the parameter passed to RequestBuilder:Post().
Perhaps, the simplest would be to just use a String variable (oRequestBody) which you initialize from a CHARACTER or LONGCHAR variable.
How do you get the value of the JSON STRING?
Are you trying to use a string literal?
Is your string multi-line? Notice that you would need to construct the string if you plan it to be multi-line.
Do you have the JSON text in a file? If so, you can use ObjectModelParser:ParseFile().
Do you need to process the data in a TEMP-TABLE or in a database table?
I hope this helps.
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.*. 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 oJsonObject AS JsonObject NO-UNDO. DEFINE VARIABLE jsonParser AS ObjectModelParser NO-UNDO. DEFINE VARIABLE oJsonEntity AS JsonObject NO-UNDO. DEFINE VARIABLE JsonString AS LONGCHAR NO-UNDO. DEFINE VARIABLE jsonReq AS CHARACTER NO-UNDO. SESSION:DEBUG-ALERT = TRUE. httpUrl = "http://localhost:7000/post". oJsonObject = new JsonObject(). oJsonObject:Add("CustNum", "200"). oJsonObject:Add("Name", "Excellent Sports Apparel"). // Exampke 1 - Direct string jsonReq ='~{"<some JSON if required for the request>"~}'. // Example 2 - Use JsonObject:GetJsonText() oRequestBody = NEW String(jsonReq). oRequestBody = NEW String(oJsonObject:GetJsonText()). // Example 3 - Use ObjectModelParser:Parse() jsonReq = '~{' + '"data":~{' + '"customers":[' + '~{' + '"id_accounting":1,' + '"id_totalgest":2,' + '"error":false,' + '"message":""' + '~},' + '~{' + '"id_accounting":3,' + '"id_totalgest":4,' + '"error":true,' + '"message":"Some reason ..."' + '~}],' + '"products":[' + '~{' + '"id_accounting":5,' + '"id_totalgest":6,' + '"error":false,' + '"message":""' + '~}],' + '"services":[' + '~{' + '"id_accounting":7,' + '"id_totalgest":0,' + '"error":true,' + '"message":"Some reason ..."' + '~}]' + '~}' + '~}'. jsonParser = NEW ObjectModelParser(). oJsonObject = CAST(JsonParser:Parse(jsonReq), JsonObject). oRequestBody = NEW String(oJsonObject:GetJsonText()). oRequest = RequestBuilder:Post(httpURL, oRequestBody) :ContentType('application/json') :AcceptJson() :Request. oResponse = ClientBuilder:Build():Client:Execute(oRequest). MESSAGE oResponse:StatusCode SKIP oResponse:StatusReason SKIP oResponse:ContentType skip oResponse:ContentLength skip vIEW-AS ALERT-BOX. oJsonEntity = CAST(oResponse:Entity, JsonObject). oJsonEntity:Write(JsonString, TRUE). MESSAGE STRING(JsonString) VIEW-AS ALERT-BOX.
With regards to creating a 'proper' json object, see https://abldojo.services.progress.com:443/#/?shareId=5dae1ed54b1a0f40c34b8c0e - note that your example json is invalid in a few ways
USING Progress.Json.ObjectModel.JsonObject. USING Progress.Json.ObjectModel.JsonArray. def var ojson as JsonObject. def var odata as JsonObject. def var ocusts as JsonArray. def var ocust as JsonObject. def var oprods as JsonArray. def var oprod as JsonObject. def var oservs as JsonArray. def var oserv as JsonObject. ojson = new JsonObject(). odata = new JsonObject(). ocusts = new JsonArray(). ocust = new JsonObject(). ocust:add( "id_accounting", 1 ). ocust:add( "id_totalgest", 2 ). ocust:add( "error", false ). ocust:add( "message", "" ). ocusts:add( ocust ). ocust = new JsonObject(). ocust:add( "id_accounting", 3 ). ocust:add( "id_totalgest", 4 ). ocust:add( "error", true ). ocust:add( "message", "Some reason ..." ). ocusts:add( ocust ). odata:add( "customers", ocusts ). oprods = new JsonArray(). oprod = new JsonObject(). oprod:add( "id_accounting", 5 ). oprod:add( "id_totalgest", 6 ). oprod:add( "error", false ). oprod:add( "message", "" ). oprods:add( oprod ). odata:add( "products", oprods ). oservs = new JsonArray(). oserv = new JsonObject(). oserv:add( "id_accounting", 7 ). oserv:add( "id_totalgest", 0 ). oserv:add( "error", true ). oserv:add( "message", "Some reason ..." ). odata:add( "services", oserv ). ojson:add( "data", odata ). def var lcc as longchar no-undo. ojson:writeFile( "data.json", true ). copy-lob from file "data.json" to lcc. message string( lcc ) view-as alert-box.
Hi Stefan,
thanks for your reply and help quickly.
Now I have understood and I will try.
many thanks.
G.
Thanks a lot for your reply.
I will try.
many thanks.
G.
Hi,
maybe you can help me. Regarding the code above. I have the dataset. How I an from dataset create a jsonfile using structure above?
CREATE redata.
ASSIGN
redata.reda_sequence = 2
redata.reda_element = "customers_deleted"
redata.reda_echarcode = tmpind.tmpi_id
redata.reda_error = TRUE
redata.reda_msg = "No record existing".
many thanks.
G.