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

Question Example Of Call To Webservice Using Openedge.net Library

Discussion in 'Development' started by altair, Aug 23, 2017.

  1. altair

    altair New Member

    Hi,

    I will begin with a development of a program which will have to call a WebService (never worked with WebService before).

    I am looking for some simple example of working code that will call an existing WebService (can be anything) via the OpenEdge.Net library.
    Basically, I need to call the WebService (providing a data) and retrieve the response (XML or JSON format).

    I need to understand how WebService works with OpenEdge.Net library.

    I just need this to work directly without any configuration (OpenEdge.Net library already included in my Propath).

    Do you have any example of such code ?

    I have already looked at examples from Progress documentation, but none of them is working fine (does not compiles and / or no result from URL/WebService)

    Thanks in advance.


    Progress & OS versions : OpenEdge 11.5.1.006, Windows 7 SP1
     
  2.  
  3. Rod Anderson

    Rod Anderson Member

    I just recently finished a project. A lot of head banging but hopefully, this will get you started.
    I wrote it on 11.6.3 but I believe it will work on your version.

    Code (progress):
    1. /*------------------------------------------------------------------------
    2.     File         Sample.p
    3.    
    4.     Purpose      Sample REST Client Request
    5.    
    6.     Author(s)    Rod Anderson
    7.   ----------------------------------------------------------------------*/
    8. ROUTINE-LEVEL ON ERROR UNDO, THROW.
    9.  
    10. USING OpenEdge.Core.String.
    11. USING OpenEdge.Net.HTTP.ClientBuilder.
    12. USING OpenEdge.Net.HTTP.IHttpClient.
    13. USING OpenEdge.Net.HTTP.IHttpRequest.
    14. USING OpenEdge.Net.HTTP.IHttpResponse.
    15. USING OpenEdge.Net.HTTP.RequestBuilder.
    16. USING OpenEdge.Net.HTTP.Credentials.
    17. USING OpenEdge.Net.MultipartEntity.
    18. USING Progress.Lang.Object.
    19. USING Progress.Json.ObjectModel.JsonArray.
    20. USING Progress.Json.ObjectModel.JsonObject.
    21. USING OpenEdge.Net.HTTP.HttpHeaderBuilder.
    22. USING OpenEdge.Net.HTTP.HttpHeader.
    23.  
    24. DEF VAR httpUrl            AS CHAR            NO-UNDO.
    25. DEF VAR oRequest           AS IHttpRequest    NO-UNDO.
    26. DEF VAR oResponse          AS IHttpResponse   NO-UNDO.
    27. DEF VAR oRequestBody       AS STRING          NO-UNDO.
    28. DEF VAR oMultiEntity       AS MultipartEntity NO-UNDO.
    29. DEF VAR oCreds             AS Credentials     NO-UNDO.
    30. DEF VAR ohttpClient        AS IHttpClient     NO-UNDO.
    31. DEF VAR oEntity            AS Object          NO-UNDO.
    32. DEF VAR oStoreList         AS jsonArray       NO-UNDO.
    33. DEF VAR hrequest           AS HANDLE          NO-UNDO.
    34. DEF VAR oJsonObject        AS jsonObject      NO-UNDO.
    35.  
    36.  
    37.  
    38. /* since JSON is spec'ed / defined as UTF-8 you will want to add
    39.      the following line if you pass a longchar as the body payload*/
    40. //fix-codepage(longchar variable) = "UTF-8".
    41.  
    42. /* very useful information to understand what is being sent and received under
    43.     the covers */
    44. SESSION:ERROR-STACK-TRACE = TRUE.
    45. SESSION:DEBUG-ALERT       = TRUE.
    46. LOG-MANAGER:LOGFILE-NAME  = SESSION:TEMP-DIR + 'request.log'.
    47. LOG-MANAGER:LOGGING-LEVEL = 6.
    48. LOG-MANAGER:CLEAR-LOG().
    49.  
    50.  
    51. httpUrl = "www.mydomain.com".
    52.  
    53.  
    54.  
    55. // Initialize the core properties
    56. ohttpClient  = ClientBuilder:Build():Client.
    57. // full REST service call
    58. httpUrl      = SUBSTITUTE("http://&1/service/v1/app/something",httpURL).
    59. //get the creditals if you need them
    60. oCreds       = NEW Credentials(httpURL, "XCDS", "abc123").
    61. // Empty payload (body) request.  You might have something in there.
    62. oRequestBody = NEW String(' ').
    63. //Build the request
    64. oRequest     = RequestBuilder:Post(httpUrl, oRequestBody)
    65.                     :UsingBasicAuthentication(oCreds)
    66.                     :AcceptJson()
    67.                     :Request.
    68. //Execute and capture the request.
    69. oResponse = ohttpClient:Execute(oRequest).
    70. // oResponse:StatusCode will probably be 200 or 201 if successful.
    71. MESSAGE oResponse:StatusCode SKIP oResponse:StatusReason VIEW-AS ALERT-BOX.
    72. //The object being returned.
    73. oEntity = oResponse:Entity.
    74.  
    75. // This gives you the Json object.  Do something with it.
    76. oJsonObject = CAST(oEntity, Jsonobject).  
     
    Cecil likes this.
  4. altair

    altair New Member

    Hi,

    Thanks, but is it possible to have a "real" URL pointing to a working Web Service ?
    Each examples I have found so far are with dummy WebService, or URL which are no more responding.

    Thanks in advance.
     
  5. Rod Anderson

    Rod Anderson Member

    Altair,

    Unauthorized Web Services calls are block for me at work but try playing around with httpbin(1): HTTP Client Testing Service. You can try all kinds of things.

    Change my previous code to something like this (disclaimer, I can't test this code from work so it may have some typo's etc).

    Code (progress):
    1.  
    2. //exchange the previous program with this code.
    3. httpUrl = "httpbin.org".
    4.  
    5. // Initialize the core properties
    6. ohttpClient  = ClientBuilder:Build():Client.
    7.  
    8. // full REST service call
    9. httpUrl      = SUBSTITUTE("http://&1/get",httpURL).
    10.  
    11. //get the creditals if you need them
    12. //oCreds       = NEW Credentials(httpURL, "XCDS", "abc123").
    13.  
    14. // Empty payload (body) request.  You might have something in there.
    15. oRequestBody = NEW String(' ').
    16. oRequest     = RequestBuilder:Get(httpUrl, oRequestBody)
    17.                   //  :UsingBasicAuthentication(oCreds)
    18.                     :AcceptJson()
    19.                     :Request.
     
  6. altair

    altair New Member

    Hi,
    I have just tried your example (just modified a little bit to accomodates with my Progress version) :



    I am getting error 5482 just as follow (with the stack trace)

    I am not sure about what to put in oRequestBody = NEW String(' '). Whatever I put here I get ane rror (if the string is not blank) so not sure it is due to the Request itself, but may be more to the connection to the URL itself.

    Here is the code :

    Code (progress):
    1. /* ***************************  Definitions  ************************** */
    2.  
    3. BLOCK-LEVEL ON ERROR UNDO, THROW.
    4.  
    5. /* ********************  Preprocessor Definitions  ******************** */
    6.  
    7.  
    8. /* ***************************  Main Block  *************************** */
    9.  
    10. USING OpenEdge.Core.String.
    11. USING OpenEdge.Net.HTTP.ClientBuilder.
    12. USING OpenEdge.Net.HTTP.IHttpClient.
    13. USING OpenEdge.Net.HTTP.IHttpRequest.
    14. USING OpenEdge.Net.HTTP.IHttpResponse.
    15. USING OpenEdge.Net.HTTP.RequestBuilder.
    16. USING OpenEdge.Net.HTTP.Credentials.
    17. USING Progress.Lang.Object.
    18. USING Progress.Json.ObjectModel.JsonArray.
    19. USING Progress.Json.ObjectModel.JsonObject.
    20. USING OpenEdge.Net.HTTP.HttpHeaderBuilder.
    21. USING OpenEdge.Net.HTTP.HttpHeader.
    22.  
    23. DEF VAR httpUrl            AS CHAR            NO-UNDO.
    24. DEF VAR oRequest           AS IHttpRequest    NO-UNDO.
    25. DEF VAR oResponse          AS IHttpResponse   NO-UNDO.
    26. DEF VAR oRequestBody       AS STRING          NO-UNDO.
    27. DEF VAR oCreds             AS Credentials     NO-UNDO.
    28. DEF VAR ohttpClient        AS IHttpClient     NO-UNDO.
    29. DEF VAR oEntity            AS Object          NO-UNDO.
    30. DEF VAR oStoreList         AS jsonArray       NO-UNDO.
    31. DEF VAR hrequest           AS HANDLE          NO-UNDO.
    32. DEF VAR oJsonObject        AS jsonObject      NO-UNDO.
    33.  
    34.  
    35.  
    36. ohttpClient  = ClientBuilder:Build():Client.
    37.  
    38. httpUrl      = "http://www.httpbin.org/get".
    39.  
    40. oRequestBody = NEW String(' ').
    41. oRequest     = RequestBuilder:Get(httpUrl, oRequestBody)
    42.                     :AcceptJson()
    43.                     :Request.
    44.  
    45.  
    46. oResponse = ohttpClient:Execute(oRequest).
    47. /* oResponse:StatusCode will probably be 200 or 201 if successful.*/
    48. MESSAGE oResponse:StatusCode SKIP oResponse:StatusReason VIEW-AS ALERT-BOX.
    49. /*The object being returned.*/
    50. oEntity = oResponse:Entity.
    51.  
    52. /* This gives you the Json object.  Do something with it.*/
    53. oJsonObject = CAST(oEntity, Jsonobject).
    54.  
     
  7. Rod Anderson

    Rod Anderson Member

    Aaltair,

    I lead you slightly astray, trying to do too many things at one time. Here are a couple of notes:

    1. I'm assuming you can get to www.httbin.org in your browser so that's not the issue. In the code below I'll show you a slightly different approach to use the URI
    2. Since it's a get request there would be no body. That's my fault. Sorry. (see code below)
    I found initially the reference documentation hard to follow due to lack of examples but as you get comfortable with it things start to make sense.

    OpenEdge API Code Documentation

    Hope that helps.

    Rod

    Code (progress):
    1.  
    2. // Add additional class at top
    3. using OpenEdge.Net.URI.
    4.  
    5. DEF VAR oUri AS URI NO-UNDO.
    6.  
    7. oUri = new URI('http', 'www.httpbin.org', 80).
    8. oUri:Path = '/get'.  // Not sure if you need the trailing slash.  You'll have to test '/get/'
    9.  
    10. oRequest     = RequestBuilder:Get(oUri)
    11.                                  :Request.
    12.  
    13.  
     

Share This Page