Question Example Of Call To Webservice Using Openedge.net Library

altair

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
 
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:
/*------------------------------------------------------------------------
    File         Sample.p
    
    Purpose      Sample REST Client Request
    
    Author(s)    Rod Anderson
  ----------------------------------------------------------------------*/
ROUTINE-LEVEL ON ERROR UNDO, THROW.

USING OpenEdge.Core.String.
USING OpenEdge.Net.HTTP.ClientBuilder.
USING OpenEdge.Net.HTTP.IHttpClient.
USING OpenEdge.Net.HTTP.IHttpRequest.
USING OpenEdge.Net.HTTP.IHttpResponse.
USING OpenEdge.Net.HTTP.RequestBuilder.
USING OpenEdge.Net.HTTP.Credentials.
USING OpenEdge.Net.MultipartEntity.
USING Progress.Lang.Object.
USING Progress.Json.ObjectModel.JsonArray.
USING Progress.Json.ObjectModel.JsonObject.
USING OpenEdge.Net.HTTP.HttpHeaderBuilder.
USING OpenEdge.Net.HTTP.HttpHeader.

DEF VAR httpUrl            AS CHAR            NO-UNDO.
DEF VAR oRequest           AS IHttpRequest    NO-UNDO.
DEF VAR oResponse          AS IHttpResponse   NO-UNDO.
DEF VAR oRequestBody       AS STRING          NO-UNDO.
DEF VAR oMultiEntity       AS MultipartEntity NO-UNDO.
DEF VAR oCreds             AS Credentials     NO-UNDO.
DEF VAR ohttpClient        AS IHttpClient     NO-UNDO.
DEF VAR oEntity            AS Object          NO-UNDO.
DEF VAR oStoreList         AS jsonArray       NO-UNDO.
DEF VAR hrequest           AS HANDLE          NO-UNDO.
DEF VAR oJsonObject        AS jsonObject      NO-UNDO.



/* since JSON is spec'ed / defined as UTF-8 you will want to add 
     the following line if you pass a longchar as the body payload*/
//fix-codepage(longchar variable) = "UTF-8".
 
/* very useful information to understand what is being sent and received under 
    the covers */
SESSION:ERROR-STACK-TRACE = TRUE.
SESSION:DEBUG-ALERT       = TRUE.
LOG-MANAGER:LOGFILE-NAME  = SESSION:TEMP-DIR + 'request.log'.
LOG-MANAGER:LOGGING-LEVEL = 6.
LOG-MANAGER:CLEAR-LOG().
 
 
httpUrl = "www.mydomain.com".

 

// Initialize the core properties
ohttpClient  = ClientBuilder:Build():Client.
// full REST service call
httpUrl      = SUBSTITUTE("http://&1/service/v1/app/something",httpURL).
//get the creditals if you need them
oCreds       = NEW Credentials(httpURL, "XCDS", "abc123").
// Empty payload (body) request.  You might have something in there.
oRequestBody = NEW String(' ').
//Build the request
oRequest     = RequestBuilder:Post(httpUrl, oRequestBody)
                    :UsingBasicAuthentication(oCreds)
                    :AcceptJson()
                    :Request.
//Execute and capture the request.
oResponse = ohttpClient:Execute(oRequest).
// oResponse:StatusCode will probably be 200 or 201 if successful.
MESSAGE oResponse:StatusCode SKIP oResponse:StatusReason VIEW-AS ALERT-BOX.
//The object being returned. 
oEntity = oResponse:Entity.

// This gives you the Json object.  Do something with it.
oJsonObject = CAST(oEntity, Jsonobject).
 

altair

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.
 
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:
//exchange the previous program with this code.
httpUrl = "httpbin.org".

// Initialize the core properties
ohttpClient  = ClientBuilder:Build():Client.

// full REST service call
httpUrl      = SUBSTITUTE("http://&1/get",httpURL).

//get the creditals if you need them
//oCreds       = NEW Credentials(httpURL, "XCDS", "abc123").

// Empty payload (body) request.  You might have something in there.
oRequestBody = NEW String(' ').
oRequest     = RequestBuilder:Get(httpUrl, oRequestBody)
                  //  :UsingBasicAuthentication(oCreds)
                    :AcceptJson()
                    :Request.
 

altair

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)
---------------------------
Error (Press HELP to view stack trace)
---------------------------
Unknown hostname www.httpbin.org. (5482)
---------------------------
OK Help
---------------------------

--> adecomm/_runcode.p at line 665 (adecomm/_runcode.r)

CallStack from ABL error object:
--> Connect OpenEdge.Net.ServerConnection.ClientSocket at line 239 (OpenEdge/Net/ServerConnection/ClientSocket.r)
Connect OpenEdge.Net.ServerConnection.ClientSocket at line 216 (OpenEdge/Net/ServerConnection/ClientSocket.r)
Execute OpenEdge.Net.HTTP.Lib.ABLSockets.ABLSocketLibrary at line 157 (OpenEdge/Net/HTTP/Lib/ABLSockets/ABLSocketLibrary.r)
Execute OpenEdge.Net.HTTP.HttpClient at line 139 (OpenEdge/Net/HTTP/HttpClient.r)
Execute OpenEdge.Net.HTTP.HttpClient at line 94 (OpenEdge/Net/HTTP/HttpClient.r)
P:\temp\p99067_Untitled2.ped at line 61 (P:\temp\p99067_Untitled2.ped)


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:
/* ***************************  Definitions  ************************** */

BLOCK-LEVEL ON ERROR UNDO, THROW.

/* ********************  Preprocessor Definitions  ******************** */


/* ***************************  Main Block  *************************** */

USING OpenEdge.Core.String.
USING OpenEdge.Net.HTTP.ClientBuilder.
USING OpenEdge.Net.HTTP.IHttpClient.
USING OpenEdge.Net.HTTP.IHttpRequest.
USING OpenEdge.Net.HTTP.IHttpResponse.
USING OpenEdge.Net.HTTP.RequestBuilder.
USING OpenEdge.Net.HTTP.Credentials.
USING Progress.Lang.Object.
USING Progress.Json.ObjectModel.JsonArray.
USING Progress.Json.ObjectModel.JsonObject.
USING OpenEdge.Net.HTTP.HttpHeaderBuilder.
USING OpenEdge.Net.HTTP.HttpHeader.

DEF VAR httpUrl            AS CHAR            NO-UNDO.
DEF VAR oRequest           AS IHttpRequest    NO-UNDO.
DEF VAR oResponse          AS IHttpResponse   NO-UNDO.
DEF VAR oRequestBody       AS STRING          NO-UNDO.
DEF VAR oCreds             AS Credentials     NO-UNDO.
DEF VAR ohttpClient        AS IHttpClient     NO-UNDO.
DEF VAR oEntity            AS Object          NO-UNDO.
DEF VAR oStoreList         AS jsonArray       NO-UNDO.
DEF VAR hrequest           AS HANDLE          NO-UNDO.
DEF VAR oJsonObject        AS jsonObject      NO-UNDO.



ohttpClient  = ClientBuilder:Build():Client.

httpUrl      = "http://www.httpbin.org/get".

oRequestBody = NEW String(' ').
oRequest     = RequestBuilder:Get(httpUrl, oRequestBody)
                    :AcceptJson()
                    :Request.


oResponse = ohttpClient:Execute(oRequest).
/* oResponse:StatusCode will probably be 200 or 201 if successful.*/
MESSAGE oResponse:StatusCode SKIP oResponse:StatusReason VIEW-AS ALERT-BOX.
/*The object being returned.*/
oEntity = oResponse:Entity.

/* This gives you the Json object.  Do something with it.*/
oJsonObject = CAST(oEntity, Jsonobject).
 
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:
// Add additional class at top
using OpenEdge.Net.URI.

DEF VAR oUri AS URI NO-UNDO.

oUri = new URI('http', 'www.httpbin.org', 80).
oUri:Path = '/get'.  // Not sure if you need the trailing slash.  You'll have to test '/get/'
   
oRequest     = RequestBuilder:Get(oUri)
                                  :Request.
 

ManxPaul

New Member
Thanks for this post.

I have never used API's and now need to call a partners API. They have provided documentation but I am unsure how to convert this into Openedge language to connect and PULL the data required.

My Partners API documentation asks for Subscription Keys, Public Tokens, ID's etc and I have the ID and email but I cannot quite figure out how to authenticate the request with Public Tokens etc. Anyone available to help?

I've already lost my hair!
 
Top