/*------------------------------------------------------------------------
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.