'RestClient is not waiting for api response or sometimes it is giving the response and sometimes it doesn't

I have deployed a python machine learning code using fastapi but sometimes it is giving me the response and sometimes it doesn't. How to force it to wait for API response every time . Please help me I'm having hard time with this.

var client = new RestClient("http://127.0.0.1:8000/predictbuy");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");
var param = new GannJson1{
         Diff_Step=V1_SStep,
         Last_Swing_Bars=V2_SSwing,
         H1= V3_SH1,
         L1=V4_SL1,
         O1=V5_SO1,
         C1=V6_SC1,
         V1=V7_SV1,
         HiLo1=V8_SHiLo1,
         H2=V9_SH2,
         L2=V10_SL2,
         O2=V11_SO2,
         C2=V12_SC2,
         V2=V13_SV2,
         HiLo2=V14_SHiLo2,
         H3=V15_SH3,
         L3=V16_SL3,
         O3=V17_SO3,
         C3=V18_SC3,
         V3=V19_SV3,
         HiLo3=V20_SHiLo3 
        };
request.AddJsonBody(param);
var response1 = client.Execute(request);
char pred1=(response1.Content)[1];


Solution 1:[1]

Please check this fucntion :

public async void Function()
{
    var client = new RestClient();
    var request = new RestRequest("http://www.google.com");
    var cancellationTokenSource = new CancellationTokenSource();

    var restResponse = await client.ExecuteAsync(request, cancellationTokenSource.Token);

   
    var result = restResponse.Content; 
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1