Note: In order to be able to run the below code, the System.Net.Http namespace must be in your current assemblies.xml file because it is NOT loaded by OpenEdge by default.
You can easily add a class to the assemblies.xml by accessing the Assembly Refrences tool from the Tools menu in the Procedure Editor. the OpenEdge >> Tools menu in Progress Developer Studio, or by running proasmref from the proenv prompt.
USING System.*.
USING System.TEXT.*.
USING System.Net.*.
USING System.Net.Http.*.
DEFINE VARIABLE cURL AS CHARACTER NO-UNDO.
DEFINE VARIABLE cData AS CHARACTER NO-UNDO.
FUNCTION AddComponent RETURNS LOGICAL ( INPUT pcURL AS CHARACTER,
INPUT pcData AS CHARACTER ):
DEFINE VARIABLE oClient AS System.Net.Http.HttpClient NO-UNDO.
DEFINE VARIABLE oContent AS System.Net.Http.HttpContent NO-UNDO.
DEFINE VARIABLE oCred AS "System.Byte[]" NO-UNDO.
DEFINE VARIABLE oMessage AS HttpResponseMessage NO-UNDO.
DEFINE VARIABLE cDescrip AS CHARACTER NO-UNDO.
DEFINE VARIABLE cResult AS CHARACTER NO-UNDO.
oClient = NEW System.Net.Http.HttpClient().
oClient:BaseAddress = NEW System.Uri(pcURL).
oCred = System.Text.UTF8Encoding:UTF8:GetBytes("username:password").
oClient:DefaultRequestHeaders:Authorization = NEW System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert:ToBase64String(oCred)).
oClient:DefaultRequestHeaders:Accept:Add(NEW System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")).
oContent = NEW StringContent(pcData, System.Text.UTF8Encoding:UTF8, "application/json").
oMessage = oClient:PostAsync(pcURL, oContent):RESULT.
IF oMessage:IsSuccessStatusCode THEN DO:
cResult = oMessage:Content:ReadAsStringAsync():RESULT.
cDescrip = cResult.
END.
END FUNCTION.