I'm having problems troubleshooting why a cancelled request in the frontend isn't reaching a backend API. In other words, sending a GET/POST request using axios works just fine. But once .abort() kicks in, the network console shows request cancelled, which is also fine, but the backend does not recognized this cancellation.

The frontend code is identical to the following: https://github.com/axios/axios/issues/4338#issuecomment-1002364900

Unfortunately, according to the backend api logs, the requests go through as normal. No canceled request or error appears in the backend logs.

On a related note, when a similar request is made from other clients, like using curl in a terminal, or from a desktop app: Postman, for example, the backend recognizes the cancellation or abrupt stoppage (like hitting Ctrl+C on a curl), and the logs show: error: timeout: context canceled

Is it just not possible for javascript's abort() (via browser) to be picked up in the backend (Go app using ctx.Done()? Is there something special a CURL request is signaling that an aborted request from the browser isn't?

1

There are 1 best solutions below

0
Noon Time On

I just verified (with local express node server proxying requests to backend API, and a sample html file that calls abort() when user presses the cancel button) that yes, it is possible. I also tried this with axios tokensource = axios.CancelToken.source(); tokensource.cancel() which also works. Then when I use AbortController inside the proxy.js,

const abortController = new AbortController();
const { signal } = abortController;

include the signal in the fetch, and call abort inside the app.post block (where app = express())

  req.on('aborted', () => {
    abortController.abort();
  });

the backend Go app picks it up and returns an error.

So the issue was the existing nginx proxy/middleware, which lacks something like the above to propagate this signal.