Salesforce

Example using System.Net.Http.HttpClient to POST to a REST Web Service

« Go Back

Information

 
TitleExample using System.Net.Http.HttpClient to POST to a REST Web Service
URL Nameexample-using-system-net-http-httpclient-to-post-to-a-rest-web-service
Article Number000131070
EnvironmentProduct: OpenEdge
Version: 10.2x, 11.x, 12.x
OS: Windows
Question/Problem Description

The below example demonstrates a usage of the System.Net.Http.HttpClient class to post data to a REST service.
 
Steps to Reproduce
Clarifying Information
Error Message
Defect Number
Enhancement Number
Cause
Resolution
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.

 
Workaround
Notes
Keyword Phrase
Last Modified Date4/24/2020 10:53 AM

Powered by