httppostinno-setuppascalwinhttprequest

Asynchronous HTTP POST request without waiting for response in Inno Setup


I have an Inno Setup Installer and need to make an API call during setup. This posts some data to a remote API.

The POST call is performed in the [Code] section using Pascal and the WinHttpRequest object. The API is ASP.Net WebAPI 2 (C#).

I have full control of all parts of the process, i.e. the Inno Setup script, it's Code section and the WebAPI.

Problem

I can make POST call synchronously without any issue, but if I set the async flag to true on the WinHttpRequest.Open() method, the .Send() method does not seem to execute at all.


procedure PostData(postural: String);
   var 
     WinHttpReq: Variant;
     ReqContent: String;
   begin
     try
       WinHttpReq := CreateOleObject('WinHttp.WinHttpRequest.5.1');
       ReqContent := 'item=value';
       WinHttpReq.Open('POST', postUrl, true);
       WinHttpReq.SetRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
       WinHttpReq.Send(ReqContent);
     except
     end;
   end;

I checked with a breakpoint in the VisualStudio Debugger, and the endpoint never gets called.

While searching here and on Google I only found various attempts for getting the response asynchronously, but I could not find a solution for this issue. I do not need the response, this is a fire-and-forget kind of API call.


Condensed Question

Why does the API not receive the call and how can I fix this?

Thanks.


Solution

  • For the asynchronous request to complete, the instance of WinHttpRequest has to stay "alive".

    For that you have to make sure at least that: