0

Asynchronous Web API Calls

  • updated 9 mths ago

Description 

LogicNets offers the WebServicesFramework to help implement web API services, and the endpoints in packages that use this framework implement web API call processing. This endpoint implementation is modeled as a logicnet.  

Used With: This function is used with the WebServicesFramework in LogicNets v8.1+. 

Request Header 

With web APIs, the endpoint logicnets return a result within a few 100 milliseconds, but processing can take several minutes or even longer—when analyzing big log files, for example. The client needs to wait until the Web API request returns, which can lead to timeouts at the client or infrastructure; for example, the default IIS timeout is 15 minutes and AWS timeout is 1 minute.  

To address this, the WebServicesFramework supports asynchronous request handling using RFC 7240: Prefer Header for HTTP (https://www.rfc-editor.org/rfc/rfc7240.html#section-4.1).

Enter Prefer: respond-async in the request header.  

GET /yourcompany/yourapi/endpoint HTTP/1.1
Prefer: respond-async
Authorization: Basic ****
Accept: */*

The WebServicesFramework detects this header and starts a background processing thread. The initial request is answered with HTTP status 202 Accepted, and the system places a status URL in the X-Location HTTP header. The client uses this to poll for status updates and to collect the result. 

HTTP/1.1 202 Accepted
LN-STATUS: 202 Accepted
X-Location: /yourcompany/async.lns?
_asyncid=abc&_session=123&pos=0&stamp=1706792124645

Note: The system does not put the redirect location in the “normal” Location header, because some web servers (e.g., IIS) adapt the HTTP status code to 302 and information gets lost. 

The system can call the status location URL multiple times, but every response returns an updated URL in the X-Location header. The status request returns HTTP status 202 Accepted while the request is still running and returns detailed status information as part of the response body when available. The detailed status is encoded in a JSON array. 

HTTP/1.1 202 Accepted
LN-STATUS: 202 Accepted
X-Location: /logicnets/async.lns?
_session=123&_asyncid=abc&pos=83&stamp=1706797435292
Content-Type: application/json
Content-Length: 105

[{"message":"Processing 1/1000","status":"In progress"},{"message":"Any message","status":"In progress"}]

When the background processing is finished the response to the Update request returns HTTP status 200 or an error status when applicable. 

HTTP/1.1 200 OK
LN-STATUS: 200 OK
Content-Type: application/json
Content-Length: ...

... 

Projects based on the WebServicesFramework have a node you can use to report progress. This is in the special dropdown menu and the node is report progress

Example

Download the Asynchronous Web API Calls example projects that demonstrate the web API asynchronous call functionality: 

  • Import the AsyncWebAPI project in your workspace and publish it. 
  • Import the AsyncWebAPITester project and run it. 
Reply Oldest first
  • Oldest first
  • Newest first
  • Active threads
  • Popular
Like Follow
  • 9 mths agoLast active
  • 12Views
  • 1 Following

Home