POST JSON

Posted by Giancarlo Alberto Somma on 21-Oct-2019 15:42

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.



All Replies

Posted by egarcia on 21-Oct-2019 20:47

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.

Posted by Stefan Drissen on 21-Oct-2019 21:13

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.

Posted by Giancarlo Alberto Somma on 22-Oct-2019 04:58

Hi Stefan,

thanks for your reply and help quickly.

Now I have understood and I will try.

many thanks.

G.

Posted by Giancarlo Alberto Somma on 22-Oct-2019 04:59

Thanks a lot for your reply.

I will try.

many thanks.

G.

Posted by Giancarlo Alberto Somma on 09-Nov-2019 07:17

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.

Posted by goo on 09-Nov-2019 08:34

I would have done what Stefan showed you.
 
If this is what you want:
{
·         version"3.1.0",
·         sync-datetime"2019-11-06 09:23:48",
·         message"",
·         messages: [ ],
·         errorfalse,
·         invoices
[
o    {
§  id_customer"26518",
§  id_ow"97",
§  nr_customer"C101",
§  company"Cliente 2",
§  rows
[
§  {
§  id_invoice_details"24399",
§  id_invoice"12859",
§  description"Riga libera",
§  total20
},
§  {
§  id_invoice_details"24391",
§  id_invoice"12859",
§  description"Riga libera",
§  total20
}
]
o    }
]
}
 
Then you would have something like this:
HeaderInfo
                Invoices
                               Rows
                Invoices
                               Rows
 
So you have
HeaderInfo:JsonObject
                Invoices:jsonArray
                                Invoice:jsonObject
                                   Rows:jsonArray
                                        Row:jsonObject
oHeaderInfo = new jsonObject().
oHeaderInfo:Add(….) all needed fields.
 
oInvoices = new ….jsonArray().
So for each Invoices you will:
  oInvoices:Add(addInvoice()).
end loop.
oHeaderInfo:Add(‘Invoices’,oInvoices)
You should now have your json object with all the stuff you need………. Good luck &#128522; PS! You have to do the same for messages ….
 
addInvoice returns jsonObject ():
  oInvoice = new jsonObject().
  oInvoice:Add(….). for all fields within invoicetable.   
 
oRows = new jsonArray(). 
Loop trough rows of invoice:  
    oRows:Add(addRows()).
  end.
oInvoice:Add(‘Rows’,oRows).
End loop.
 
addRows returns jsonObject:
  oRow = new jsonObject().
  oRow:Add(…) for all fields within row.
end.
 

This thread is closed