1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Answered Sending Json As Form-data

Discussion in 'Development' started by Wolfgang Schölmberger, Oct 6, 2017.

  1. After fighting last week to receive JSON-data via httpGet I ran into the next problem immediatelly. Now I have to send back this modified JSON-data, but it seems that I have to send it back as form-data. How would I do that ?

    oRequest = RequestBuilder:post(httpUrl, JsonObject)
    :ContentType('application/json')
    :AcceptJson()
    :Request.
     
  2.  
  3. Cecil

    Cecil 17+ years progress programming and still learning.

    Doing a 'POST' request is of type 'form-data'. The example ABL code you provided looks correct.

    Also, try putting the following after the the request to test that is was submitted correctly:

    Code (progress):
    1. oResponse = ClientBuilder:Build():Client:Execute(oRequest).
    2. MESSAGE
    3.     oResponse:StatusCode SKIP
    4.     oResponse:StatusReason SKIP
    5. VIEW-AS ALERT-BOX.
     
    Last edited: Oct 9, 2017
  4. Cecil

    Cecil 17+ years progress programming and still learning.

    Also, looking at the documenetation there is the following:

     
  5. i managed to do the POST using Postman, but i'm still not able to "translate" this to proper ABL-code. Maybe somebody could help.
    Attatched a screen-shot how I did it in Postman httpPost.gif
     
  6. Cecil

    Cecil 17+ years progress programming and still learning.

    So the form field name (or key) is 'data' and the form field value is a JSON string?
     
  7. i guess yes, but as I said before i'm completely new to this programming. Searching the OpenEdge-documentation, gives me just 1 hit, where "AddFormData" ist only mentioned, nothing more.
    So i'm kind of stuck....
     
  8. Cecil

    Cecil 17+ years progress programming and still learning.

    Do you have any developer documentation you can share?
     
  9. sorry, the only "documentation" i got was the following email:

    a new order can be created with the following link:

    Link: http://datapad.it-park.at/projekte/datapad/immoware/datapad2ext.nsf/xp_httpPOST.xsp?u=connect/immoware&p=29immoware29!&anr=12345

    Body:
    {
    "wf_id":"12345",
    "wf_nr":"13245",
    "wf_bez":"Immoware 12345",
    "wf_titel":"Tablet-Übermittlung WF: 12345",
    "wf_betreff":"Datapad V1",
    "wf_start":"2017-06-30",
    "wf_frist":"2017-07-01",
    "txtUsername":"test/immoware",
    "txtFormID":"4",
    "obj_eig_strasse":"Kärntnerstrasse 12",
    "obj_eig_name":"Immoware GmbH",
    "obj_eig_adresse":"1010 Wien",
    "obj_kan_nr":1,
    "obj_kli_nr":12,
    "obj_ap_telefon":null,
    "obj_ap_tel":null,
    "obj_ap_email":null,
    "Beginndatum":"2017-07-01",
    "Ersteller":"Wolfgang Schölmberger"
    }
     
  10. Cecil

    Cecil 17+ years progress programming and still learning.

    This seems to work.

    This code needs to be refactored.

    Code (progress):
    1. USING OpenEdge.Net.URI.
    2. using OpenEdge.Net.HTTP.IHttpClient.
    3. USING OpenEdge.Net.HTTP.IHttpRequest.
    4. USING OpenEdge.Net.HTTP.IHttpResponse.
    5. USING OpenEdge.Net.HTTP.RequestBuilder.
    6. USING OpenEdge.Net.HTTP.ClientBuilder.
    7. USING Progress.Lang.Object.
    8. USING Progress.Json.ObjectModel.JsonArray.
    9. USING Progress.Json.ObjectModel.JsonObject.
    10. USING Progress.Json.JsonParser.
    11. USING OpenEdge.Net.UriSchemeEnum.
    12. using OpenEdge.Core.String.
    13.  
    14. DEFINE VARIABLE oURI        AS URI                 NO-UNDO.
    15. DEFINE VARIABLE httpUrl     AS CHARACTER           NO-UNDO.
    16. DEFINE VARIABLE oRequest    AS class IHttpRequest  NO-UNDO.
    17. DEFINE VARIABLE oResponse   AS class IhttpResponse NO-UNDO.
    18. DEFINE VARIABLE oEntity     AS class Object        NO-UNDO.
    19. DEFINE VARIABLE oJson       AS class JsonObject    NO-UNDO.
    20.  
    21. DEFINE VARIABLE userRecordJSON AS LONGCHAR NO-UNDO.
    22. define variable httpClient as IHttpClient no-undo.
    23.  
    24.  
    25. log-manager:logging-level = 6.
    26. log-manager:logfile-name = session:temp-dir + 'get_applications.log'.
    27. log-manager:clear-log().
    28.  
    29.  
    30. /** Lets Build the JSON object from scratch. In reality, this would
    31.     already be available to you from your previous GET request. **/
    32.  
    33. oJson = NEW JsonObject().
    34.  
    35. oJson:Add("wf_id", "12345").
    36. oJson:Add("wf_nr", "13245").
    37. oJson:Add("wf_bez", "Immoware 12345").
    38. oJson:Add("wf_titel", "Tablet-Übermittlung WF: 12345").
    39. oJson:Add("wf_betreff", "Datapad V1").
    40. oJson:Add("wf_start", "2017-06-30").
    41. oJson:Add("wf_frist", "2017-07-01").
    42. oJson:Add("txtUsername", "test/immoware").
    43. oJson:Add("txtFormID", "4").
    44. oJson:Add("obj_eig_strasse","Kärntnerstrasse 12").
    45. oJson:Add("obj_eig_name", "Immoware GmbH").
    46. oJson:Add("obj_eig_adresse", "1010 Wien").
    47. oJson:Add("obj_kan_nr", 1).
    48. oJson:Add("obj_kli_nr", 12).
    49. oJson:Add("obj_ap_telefon", integer(?) ).  /** NULL **/
    50. oJson:Add("obj_ap_tel", integer(?) ). /** NULL **/
    51. oJson:Add("obj_ap_email", integer(?) ). /** NULL **/
    52. oJson:Add("Beginndatum", "2017-07-01").
    53. oJson:Add("Ersteller", "Wolfgang Schölmberger").
    54.  
    55. /** Write the JSON objec to a longchar.
    56.    'False' option for no formatting. i.e. make a
    57.     single long string with not CR & LF */
    58.  
    59. oJson:write(input-output userRecordJSON, false).
    60.  
    61.  
    62. /** Let's build the URL/URI Query String using OPENEDGE classes i.e. http://.....**/
    63.  
    64. oURI = new URI("http",  "datapad.it-park.at").
    65. oURI:Path   = '/projekte/datapad/immoware/datapad2ext.nsf/xp_httpPOST.xsp'.
    66. oURI:AddQuery('u', 'connect/immoware').
    67. oURI:AddQuery('p', '29immoware29!').
    68. oURI:AddQuery('anr', '12345').
    69.  
    70. oRequest = RequestBuilder:Build('POST', oURI )
    71.                 :AcceptJson() /* As the HTTP client, we accepet messages back in form a JSON */
    72.                 :AddFormData("data", string(userRecordJSON) )  /** We have to limit the longchar down to 1024 bytes. AddFormData does not allow LongChar datatypes. **/
    73.                 :Request.
    74.              
    75. oResponse = ClientBuilder:Build():Client:Execute(oRequest).
    76.            
    77. /** Debug messages... **/          
    78.              
    79. MESSAGE
    80.     "Status Code: " oResponse:StatusCode SKIP
    81.     "Status Reason: " oResponse:StatusReason SKIP
    82.     "Character Encoding: " oResponse:CharacterEncoding SKIP
    83.     "Content Type: " oResponse:ContentType SKIP
    84.     "Transfer Encoding: " oResponse:TransferEncoding skip
    85.     "Server Response " skip(1)
    86.     oResponse:Entity
    87.     VIEW-AS ALERT-BOX info.
    88.  
    89. /** Write the server response to a JSON file..**/
    90. oEntity = oResponse:Entity.
    91.  
    92. IF TYPE-OF(oEntity, JsonObject) THEN
    93.     CAST(oEntity, JsonObject):WriteFile("./postResponce.json", true).
    94.  
    95. /** NOTE: the Lotus-Domino server is returning the content type and text/html when in fact it's application/JSON **/
    96.  
    97.  
    98.  
     
    Last edited: Oct 11, 2017
  11. 1000 thanks Cecil, it works like a charm !!!!
     
    Cecil likes this.
  12. Cecil

    Cecil 17+ years progress programming and still learning.

    PLEASE MAKE A NOTE LIMITATION OF THE FORM FIELD (JSON string) LIMITATION OF BEING 1024 BYTES.

    I must admit, I find the http.net.pl not very intuitive.
     

Share This Page