All requests have two mandatory elements: an HTTP verb method (GET, for example) and a URI (the address for the request).

Some requests also require a payload (also called the entity, body, or message). To add a payload to a request, set the Entity property to an object instance. All entity data must be held in an object instance. Headers can be added to the request by adding the SetHeader() method.

Request objects can be used by multiple requests.

Building a request

The OpenEdge.Net.Http.RequestBuilder helper class provides a fluent (or chained) API for creating IHttpRequest objects.

The initial methods to call are one of the following static HTTP verb methods:

  • RequestBuilder:Get ( uri [, entity ])
  • RequestBuilder:Put ( uri, entity )
  • RequestBuilder:Post ( uri, entity )
  • RequestBuilder:Delete ( uri [, entity ])
  • RequestBuilder:Head ( uri )
  • RequestBuilder:Options ( uri [, entity ])
  • RequestBuilder:Trace ( uri )

Where uri is either an instance of a URI object or a character string, and entity is an instance of an object.

After an initial method is called, a number of additional methods can be chained, including:

  • AddHeader(): Add custom headers.
  • Accept* : Methods to add media types to the Accept header.

  • HttpVersion : Change the HTTP version from its 1.1 default.
  • AddJson( entity ), AddFormData( data ), WithData( entity ), and WithData( entity, content type ) : Add JSON, Form or other data to the request.
  • UsingBasicAuthentication, UsingDigestAuthentication, and UsingCredentials : Add credentials to the request.
    Note:

    The more general UsingCredentials does not resolve a challenge. See https://tools.ietf.org/html/rfc2617 for information about challenges.

    Also note that credentials are only retrieved when the request is being executed.

  • AuthCallback : Add a listener to authentication events from a request.
  • ContentType : Indicates the content type of the entity being sent with a request.

Once the request is built, it must be returned by calling the Request property. It can then be passed into the HTTPClient for execution.

Examples

The following code samples contain examples of using RequestBuilder.

Basic Get

USING OpenEdge.Net.HTTP.RequestBuilder.
USING OpenEdge.Net.HTTP.IHttpRequest.

DEFINE VARIABLE httpUrl AS CHARACTER NO-UNDO.
DEFINE VARIABLE oRequest AS IHttpRequest NO-UNDO.

httpUrl = "http://www.progress.com".
 oRequest = RequestBuilder:Get(httpUrl)
                   :Request.

Simple PUT

USING OpenEdge.Net.HTTP.RequestBuilder.
USING OpenEdge.Net.HTTP.IHttpRequest.
USING Progress.Json.ObjectModel.JsonObject.

DEFINE VARIABLE httpUrl AS CHARACTER NO-UNDO.
DEFINE VARIABLE oRequest AS IHttpRequest NO-UNDO.
DEFINE VARIABLE oJson AS JsonObject NO-UNDO.

httpUrl = 
"http://oemobiledemo.progress.com/VehicleOrderService/rest/VehicleOrder/Cart". 
oJson = new JsonObject().
/* assume that there's some real data in this object */
oJson:Add('dsShoppingCart', new JsonObject()). 

oRequest = RequestBuilder:Put(httpUrl, oJson) 
                :AcceptJson() /* we want to get JSON back */ 
                :Request.

Adding Basic Authentication

USING OpenEdge.Net.HTTP.Credentials. 

DEFINE VARIABLE oCredentials AS Credentials NO-UNDO.
DEFINE VARIABLE httpUrl AS CHARACTER NO-UNDO. 

httpUrl = "http://localhost:9090/oem/resources". 

oCredentials = new Credentials('application', 'admin', '4admin'). 

oRequest = RequestBuilder:Get(httpUrl) 
                :AcceptJson()
                :UsingBasicAuthentication(oCredentials)
                :Request.

Adding general credentials

oCredentials = new Credentials('application', 'admin', '4admin'). 

oRequest = RequestBuilder:Get('http://localhost:9090/oem/resources')
                :AcceptJson()
                :UsingCredentials(oCredentials)
                :Request.

Adding Auth Filter Callback as a class

USING OpenEdge.Net.HTTP.Filter.Auth.IAuthFilterEventHandler.
USING OpenEdge.Net.HTTP.Credentials.
 

oRequest = RequestBuilder:Get('http://localhost:9090/oem/resources')
                :AcceptJson()
                :AuthCallback(new AuthStatusListener())
                :Request. 

/* The AuthStatusListener class is defined as: */
CLASS AuthStatusListener implements IAuthFilterEventHandler:

  METHOD PUBLIC VOID AuthFilter_HttpCredentialRequestHandler(
      INPUT poSender as Object,
      INPUT poEventArgs as AuthenticationRequestEventArgs ):
 
  poEventArgs:Credentials = new 
  Credentials('domain','admin','4admin').        
  END METHOD.

END CLASS.

Adding Auth Filter Callback as a procedure

USING OpenEdge.Net.HTTP.AuthenticationRequestEventArgs.
USING OpenEdge.Net.HTTP.Credentials. 

oRequest = RequestBuilder:Get('http://localhost:9090/oem/resources')
                :AcceptJson()
                :AuthCallback(this-procedure) 
                :Request. 

PROCEDURE AuthFilter_HttpCredentialRequestHandler: 
    DEFINE INPUT PARAMETER poSender as Object. 
    DEFINE INPUT PARAMETER poEventArgs as AuthenticationRequestEventArgs.

    poEventArgs:Credentials = new 
    Credentials('domain','admin','4admin'). 

END PROCEDURE.