HTTP request to an external web resource
Ancestors - Object (PRIM_OBJT)
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.
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
Name | Description |
---|---|
ComponentClassName | ComponentClassName is the name of the component's class. Inherited from Object (PRIM_OBJT) |
ComponentMembers | ComponentMembers provides access to all the member components of this component Inherited from Object (PRIM_OBJT) |
ComponentPatternName | ComponentPatternName is used to qualify the class of the component. Inherited from Object (PRIM_OBJT) |
ComponentTag | Generic space allowing a value to be stored for the instance Inherited from Object (PRIM_OBJT) |
ComponentType | ComponentType gives you access to the type information about the component Inherited from Object (PRIM_OBJT) |
ComponentTypeName | ComponentTypeName is the fully qualified name of the component's class. Inherited from Object (PRIM_OBJT) |
Content | The content of an HTTP request |
Headers | HTTP Headers |
Method | Standard HTTP request methods such as GET, POST etc. |
Name | Name identifies the component Inherited from Object (PRIM_OBJT) |
Owner | Owner owns this component Inherited from Object (PRIM_OBJT) |
Parent | The 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) |
Response | Reference to an HTTP Response object returned by the request |
Status | Standard HTTP request status code |
Timeout | Standard HTTP request timeout |
Url | URL of the request |
Name | Description |
---|---|
Completed | Fired when the HTTP request returns after processing successfully |
CreateInstance | CreateInstance is signalled when an instance of a component is created Inherited from HTTP Request (PRIM_WEB.HttpRequest) |
DestroyInstance | DestroyInstance is signalled when an instance of a component is about to be destroyed Inherited from HTTP Request (PRIM_WEB.HttpRequest) |
Failed | Fired when the HTTP request returns after processing unsuccessfully |
Name | Description |
---|---|
Cancel | Cancel the HTTP request |
Execute | Execute the Request synchronously |
ExecuteAsync | Execute the request asynchronously |
Febuary 18 V14SP2