Close HTTP request when using ArduinoHttpClient library

Hello Everyone,

My project involves using the arduino MKRNB 1500 board to act as a USBHost and get data from a barcode scanner and send the data to a webserver by making a POST request. Additionally, I am sending that data to a different device using Serial1. The code works without any issues when the server is up and running. In order to test the response when we receive a timeout error I am manually adding 10s timeout. However, even after receiving the timeout error code (-3) the connection is still alive as a result my arduino stops. Because of the timeout I am not able to make further scans. I would like to stop the request after timeout such that I can do other operations if the POST request fails after 10s. Is it possible to stop requests after the timeout using this library. I have added the code I am using, please let me know if you need more information from my side.

#include <MKRNB.h>
#include <ArduinoHttpClient.h>
#include <ArduinoJson.h>
#include <KeyboardController.h>

#if (USB_VID==0x2341 && defined(ARDUINO_SAMD_ZERO)) || (USB_VID==0x2a03 && defined(ARDUINO_SAM_ZERO))
#define SerialDebug SERIAL_PORT_MONITOR
#else
#define SerialDebug Serial1
#endif


const char PINNUMBER[]  = "";


NBClient client;
GPRS gprs;
NB nbAccess;
USBHost usb;
KeyboardController keyboard(usb);
String currentBarcode;


char server[] = "";
char path[] = "";
int port = 80;

StaticJsonDocument<200> jsonBuffer;
//HttpClient httpClient = HttpClient(client, server, port);
JsonObject root = jsonBuffer.to<JsonObject>();
int count = 0;


void sendBarcode(String myBarcode){
  root["boxid"] = "Test";
  root["gtin"] = myBarcode;
  
  SerialDebug.println(myBarcode);

  if (client.connect(server, port)) {
    postToWia(root);
  } else {
  }

  if (!client.available() && !client.connected()) {
    SerialDebug.println(("Disconnected"));
    client.stop();
  }

}

void keyPressed() {
  if ( keyboard.getOemKey() == 40 ){
    sendBarcode(currentBarcode);
    currentBarcode = "";
  }
  else{
    currentBarcode += String((char)keyboard.getKey());
  }
}

void setup()
{
  SerialDebug.begin(9600);

  boolean connected = false;

  while ((!connected) && (count<=1)) {
    SerialDebug.println(count);
    if ((nbAccess.begin(PINNUMBER) == NB_READY) && (gprs.attachGPRS() == GPRS_READY)) {
      connected = true;
    } else {
      SerialDebug.println("Not connected");
      count++;
    }
  }
  currentBarcode = "";
}


void loop()
{
  usb.Task();
}


void postToWia(JsonObject& data) {
  HttpClient httpClient = HttpClient(client, server, port);
  String dataStr = ""; 
  serializeJson(data, dataStr);
  httpClient.setHttpResponseTimeout(10000);
  httpClient.beginRequest();
  httpClient.post(path);
  httpClient.sendHeader("Content-Type", "application/json");
  httpClient.sendHeader("Content-Length", dataStr.length());
  //httpClient.sendHeader("Authorization", "Bearer " + String(device_secret_key));
  httpClient.beginBody();
  httpClient.print(dataStr);  
  httpClient.endRequest();
  SerialDebug.println(httpClient.responseStatusCode()); // returns -3 after 10s
  httpClient.stop();
}

the ArduinoHttpClient has no way to set a write timeout.
you can do the POST request without the library

Thank you for the response, I fixed this without needing the ArduinoHttpClient library.