Feedback
Did this article resolve your question/issue?

   

Article

How to post a FORM-ENCODED message using OpenEdge HttpClient?

« Go Back

Information

 
TitleHow to post a FORM-ENCODED message using OpenEdge HttpClient?
URL NameHow-to-post-a-FORM-ENCODED-message-using-OpenEdge-HttpClient
Article Number000122847
EnvironmentProduct: OpenEdge
Version: 11.5.1, 11.6.x, 11.7.x, 12.x
OS: All Supported Platforms
Other: HttpClient
Question/Problem Description
The below example code demonstrates executing a POST request which posts a form encoded message.
 
Steps to Reproduce
Clarifying Information
Error Message
Defect Number
Enhancement Number
Cause
Resolution
/*------------------------------------------------------------------------
    File        : post_form_name_value.p
    Purpose     : POST a form-encoded message based on a series of name-value paris
    Author(s)   : pjudge
    Created     : 2018-08-17
    Notes       :
  ----------------------------------------------------------------------*/
BLOCK-LEVEL ON ERROR UNDO, THROW.

USING OpenEdge.Core.Collections.IStringStringMap.
USING OpenEdge.Core.Collections.StringStringMap.
USING OpenEdge.Net.HTTP.ClientBuilder.
USING OpenEdge.Net.HTTP.Credentials.
USING OpenEdge.Net.HTTP.IHttpClient.
USING OpenEdge.Net.HTTP.IHttpRequest.
USING OpenEdge.Net.HTTP.IHttpResponse.
USING OpenEdge.Net.HTTP.RequestBuilder.
USING Progress.Json.ObjectModel.JsonObject.

/* ***************************  Definitions  ************************** */
DEFINE VARIABLE oClient        AS IHttpClient   NO-UNDO.
DEFINE VARIABLE oRequest       AS IHttpRequest  NO-UNDO.
DEFINE VARIABLE oResponse      AS IHttpResponse NO-UNDO.
DEFINE VARIABLE oCredentials   AS Credentials   NO-UNDO.
DEFINE VARIABLE cURL           AS CHARACTER     NO-UNDO.
DEFINE VARIABLE cClientId      AS CHARACTER     NO-UNDO.
DEFINE VARIABLE cClientSecret  AS CHARACTER     NO-UNDO.
DEFINE VARIABLE oJsonData      AS JsonObject    NO-UNDO.
DEFINE VARIABLE oStringMap     AS StringStringMap NO-UNDO.
/* ***************************  Main Block  *************************** */

ASSIGN cClientId     = 'myuserid'
       cClientSecret = 'mypassword'
       cURL          = 'http://httpbin.org/post'
       oClient       = ClientBuilder:Build():Client
       oCredentials  = NEW Credentials('mydomain':u, cClientId, cClientSecret).
       
// build the request

oStringMap = NEW StringStringMap().
oStringMap:PUT('client_id', 'oidc_ovf_conf').
oStringMap:PUT('grant_type','authorization_code').

oRequest = RequestBuilder:Post(cURL,
                          oStringMap )
                         :UsingBasicAuthentication(oCredentials)
                         :Request.

// make the request
oResponse = oClient:Execute(oRequest).

// process the response
MESSAGE oResponse:StatusCode         SKIP   // 200 is all went well
        oResponse:ContentType        SKIP   // something like application/json or application/x-www-form-urlencoded
        oResponse:ContentLength      SKIP   // number of bytes, if you care
        
    VIEW-AS ALERT-BOX.

// this Entity property is DEFINED as Progress.Lang.Object
// but the actual class/type will be something else; typically something that matches the ContentType 
// now get the response data in  a nice, strongly-typed Object form
// We can either decide what to do based on the ContentType or on the object's type

// approach 1
CASE oResponse:ContentType:
    WHEN 'application/json':u THEN
        oJsonData = CAST(oResponse:Entity, JsonObject).
    // other types    
END CASE.

// approach 2
CASE TRUE:
    WHEN TYPE-OF(oResponse:Entity, JsonObject) THEN
        oJsonData = CAST(oResponse:Entity, JsonObject).    
    // other types
END CASE.

// do what you need to with the response data
IF VALID-OBJECT(oJsonData) THEN
DO:
    MESSAGE STRING(oJsonData:GetJsonText())
        VIEW-AS ALERT-BOX INFORMATION BUTTONS OK.
END.

CATCH oError AS Progress.Lang.Error:
    MESSAGE oError
        VIEW-AS ALERT-BOX.
END CATCH.

 
Workaround
Notes
References to Other Documentation:

Progress Article(s):
 Is there an HTTP client for OpenEdge?
 OpenEdge.Net.pl is missing from PROPATH
 
Last Modified Date11/23/2021 5:52 PM
Attachment 
Files
Disclaimer The origins of the information on this site may be internal or external to Progress Software Corporation (“Progress”). Progress Software Corporation makes all reasonable efforts to verify this information. However, the information provided is for your information only. Progress Software Corporation makes no explicit or implied claims to the validity of this information.

Any sample code provided on this site is not supported under any Progress support program or service. The sample code is provided on an "AS IS" basis. Progress makes no warranties, express or implied, and disclaims all implied warranties including, without limitation, the implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the sample code is borne by the user. In no event shall Progress, its employees, or anyone else involved in the creation, production, or delivery of the code be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample code, even if Progress has been advised of the possibility of such damages.