HTTP Request (PRIM_WEB.HttpRequest)

HTTP request to an external web resource

Ancestors - Object (PRIM_OBJT)

Details

An HTTPRequest allows access to 3rd party HTTP services, allowing LANSA to integrate with other web services.
 
An HTTPRequest is effectively a standard XMLHttpRequest in JavaScript.
 
HTTPRequests should be defined within the routine in which they are going to execute. This will create a new instance every time one is executed. The corresponding event routines for the Completed and Failed events are coded in the same routine.
 
The LANSA runtime maintains the instances until they have finished. This allows the same routine to be executed many times, and many requests to be performed, each one independently producing results.
 
If the HTTPRequest exists as a global scope, there will only ever be one instance, meaning multiple requests may interfere with each other.
 
Therefore, HTTPRequest instances should not be defined within an EVTROUTINE as event routines cannot have other child event routines to handle the Completed and Failed events. Define and call the HTTPRequest instance in a method routine, which is called from the event routine instead.

Example

In this example, a weather API is executed asynchronously, returning the data as a JSON object.
 
When the completed event fires, the result is processed.
Mthroutine Name(GetWeather) Access(*PRIVATE)
Define_Map For(*INPUT) Class(#Prim_Str) Name(#Resource)

Define_Com Class(#PRIM_WEB.HttpRequest) Name(#Request)
Define_Com Class(#PRIM_WEB.Json) Name(#Json)

#Request.Url := "http://api.openweathermap.org" + #Resource

#Com_owner.UpdateForPending

#Request.ExecuteAsync

Evtroutine Handling(#Request.Completed)

If (#Request.Response *IsNot *null)

Case (#Request.Response.StatusCode)
When (= 200)

#Json := #Request.Response.Content
#Com_owner.Update( #Json.RootItem )

Otherwise

#Com_owner.UpdateForFailure( #Request )

Endcase

Endif

Endroutine

Endroutine

Properties

NameDescription
ComponentClassNameComponentClassName is the name of the component's class. Inherited from Object (PRIM_OBJT)
ComponentMembersComponentMembers provides access to all the member components of this component Inherited from Object (PRIM_OBJT)
ComponentPatternNameComponentPatternName is used to qualify the class of the component. Inherited from Object (PRIM_OBJT)
ComponentTagGeneric space allowing a value to be stored for the instance Inherited from Object (PRIM_OBJT)
ComponentTypeComponentType gives you access to the type information about the component Inherited from Object (PRIM_OBJT)
ComponentTypeNameComponentTypeName is the fully qualified name of the component's class. Inherited from Object (PRIM_OBJT)
ContentThe content of an HTTP request
HeadersHTTP Headers
MethodStandard HTTP request methods such as GET, POST etc.
NameName identifies the component Inherited from Object (PRIM_OBJT)
OwnerOwner owns this component Inherited from Object (PRIM_OBJT)
ParentThe component instance to which this instance is attached. The visual container for a control or the collector of a set of child instances Inherited from Object (PRIM_OBJT)
ResponseReference to an HTTP Response object returned by the request
StatusStandard HTTP request status code
TimeoutStandard HTTP request timeout
UrlURL of the request

Events

NameDescription
CompletedFired when the HTTP request returns after processing successfully
CreateInstanceCreateInstance is signalled when an instance of a component is created Inherited from HTTP Request (PRIM_WEB.HttpRequest)
DestroyInstanceDestroyInstance is signalled when an instance of a component is about to be destroyed Inherited from HTTP Request (PRIM_WEB.HttpRequest)
FailedFired when the HTTP request returns after processing unsuccessfully

Methods

NameDescription
CancelCancel the HTTP request
ExecuteExecute the Request synchronously
ExecuteAsyncExecute the request asynchronously

See also

All Component Classes

Technical Reference

Febuary 18 V14SP2