Prior to OpenEdge 11.5.1, which provides's built in support for consuming REST webservices, sockets can be used.
The following pseudo code attached to this article [
PostHTTPRequestUsing4GLSockets.p ]
It uses the POST method to send the request content to a Web server URL and waits for its reply.
1. Connects to the Server, using a hostname and port
2. Sends a piece of data, consisting of:
- The URL that will be dealing the request, the sample uses a webspeed URL, but could it be anything in the other side as long as it is HTTP. Sonic http listeners are good example of real needs for usage of this technique
- The data in the format: param=value¶m=value¶m=value.
3. Gets the answer in the procedure getResponse, which can be customized to read, parse and work with the results.
Only basic headers are sent, which can be extended to support headers like cookies, different content types
Refer to the documentation and related Articles below to extend this sample procedure and make it more powerful:
DEFINE VARIABLE vcHost AS CHARACTER INITIAL "localhost" NO-UNDO.
DEFINE VARIABLE vcPort AS CHARACTER INITIAL "8080" NO-UNDO.
DEFINE VARIABLE vhSocket AS HANDLE NO-UNDO.
CREATE SOCKET vhSocket.
vhSocket:CONNECT('-H ' + vcHost + ' -S ' + vcPort) NO-ERROR.
IF vhSocket:CONNECTED() = FALSE THEN
DO:
MESSAGE "Connection failure" VIEW-AS ALERT-BOX.
MESSAGE ERROR-STATUS:GET-MESSAGE(1) VIEW-AS ALERT-BOX.
RETURN.
END.
ELSE
MESSAGE "Connect"
VIEW-AS ALERT-BOX.
vhSocket:SET-READ-RESPONSE-PROCEDURE('getResponse').
/* supposes there is an webspeed app called yourapp.w that receives param1, param2, param3 */
RUN PostRequest (
INPUT '/scripts/cgiip.exe/WService=wsbroker1/yourApp.w',
INPUT 'param1=value¶m2=value¶m3=value'
).
WAIT-FOR READ-RESPONSE OF vhSocket.
vhSocket:DISCONNECT() NO-ERROR.
DELETE OBJECT vhSocket.
QUIT.
PROCEDURE getResponse:
DEFINE VARIABLE vcWebResp AS CHARACTER NO-UNDO.
DEFINE VARIABLE lSucess AS LOGICAL NO-UNDO.
DEFINE VARIABLE mResponse AS MEMPTR NO-UNDO.
IF vhSocket:CONNECTED() = FALSE THEN do:
MESSAGE 'Not Connected' VIEW-AS ALERT-BOX.
RETURN.
END.
lSucess = TRUE.
DO WHILE vhSocket:GET-BYTES-AVAILABLE() > 0:
SET-SIZE(mResponse) = vhSocket:GET-BYTES-AVAILABLE() + 1.
SET-BYTE-ORDER(mResponse) = BIG-ENDIAN.
vhSocket:READ(mResponse,1,1,vhSocket:GET-BYTES-AVAILABLE()).
vcWebResp = vcWebResp + GET-STRING(mResponse,1).
END.
/*
*PUT HERE THE CODE TO MANIPULATE THE ANSWER
*/
PROCEDURE PostRequest:
DEFINE VARIABLE vcRequest AS CHARACTER.
DEFINE VARIABLE mRequest AS MEMPTR.
DEFINE INPUT PARAMETER postUrl AS CHAR.
/* URL that will send the data. It must be all the path after the server. IE:/scripts/cgiip.exe/WService=wsbroker1/myApp.htm */
DEFINE INPUT PARAMETER postData AS CHAR.
/* Parameters to be sent in the format paramName=value¶mName=value¶mName=value */
vcRequest =
'POST ' +
postUrl +
' HTTP/1.0~r~n' +
'Content-Type: application/x-www-form-urlencoded~r~n' +
'Content-Length:' + string(LENGTH(postData)) +
'~r~n' + '~r~n' +
postData + '~r~n'.
MESSAGE vcREquest VIEW-AS ALERT-BOX.
SET-SIZE(mRequest) = 0.
SET-SIZE(mRequest) = LENGTH(vcRequest) + 1.
SET-BYTE-ORDER(mRequest) = BIG-ENDIAN.
PUT-STRING(mRequest,1) = vcRequest .
vhSocket:WRITE(mRequest, 1, LENGTH(vcRequest)).
END PROCEDURE.