[Progress Communities] [Progress OpenEdge ABL] Forum Post: RE: POST JSON

Status
Not open for further replies.
E

egarcia

Guest
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: - 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 ='~{" "~}'. // 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.

Continue reading...
 
Status
Not open for further replies.
Top