0

The following JavaScript code sends a file to an embedded LWIP HTTP server in chunks.

As can be seen in the Wireshark trace, a reset sometimes occurs during transmission. In this case, the request is aborted, however the script does not receive any information from this and returns true even in case of an error.

Does anyone have a tip on how to teach the script to recognize such an error?

The function should only return true if the request was successful, otherwise the call will repeated again with the same chunk.

reset while http post

async function uploadChunk(chunk) {
  let result = false;
  const formData = new FormData();
  formData.append("file", chunk);
  sleep(10);
  
  try {
    const response = await fetch("http://" + target_ip + "/update", {
      method: "POST",
      mode: "no-cors",
      body: formData
    });
    res = await response;
    const ok_string = "OK";
    result = (response.ok & (response.statusText === ok_string));
    console.log(res);
  } catch (error) {
    console.error("Failed to upload chunk: ", error);
  }
  
  return result;
}
7
  • A fetch only throws an error if a network-level failure happens. You need to implement response handling yourself. Start by checking the response code - similar to how you already do with response.ok and response.statusText Commented Jul 31, 2024 at 10:24
  • res = await response; looks odd Commented Jul 31, 2024 at 10:27
  • he function should only return true the function won't ever return true ... it could return false, but it could also return 0 or 1 ... but never true - because true & false is 0 and true & true is 1 - did you mean response.ok && .....etc? - are you checking if the returned value is false and assuming if it's not then there was no issue? Commented Jul 31, 2024 at 10:31
  • The first version uses only result = response.ok; The (wrong) logic AND was only inserted out of pure depreciation. According to which criterion can i distinguish whether the fetch was successful? I thought it is response.ok != 0 Commented Jul 31, 2024 at 12:09
  • 1
    Javascript can’t read the response if you specify mode:no-cors in the request. Commented Jul 31, 2024 at 12:29

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.