question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cancel works for get but not post

See original GitHub issue

Hi, I found cancel works for get but not post. I can receive 500 with cancel for get, but not post. The code is as below:

get(url, params, headers, jsbaLogger, logName, responseCallBack, errorCallBack){
            var clientStart = Date.now();
            var CancelToken = axios.CancelToken;
            var source = CancelToken.source();
            var data = {};
            data.params = params;
            data.cancelToken = source.token;

            if(headers==null){
                headers = {};
            }

            axios.get(url, data, headers,timeout)
                .then((response) => {
                if(jsbaLogger){
                    jsbaLogger.logHttpRequest(logName,
                        clientStart,
                        response.data.time ? response.data.time.serverStart : undefined,
                        response.data.time ? response.data.time.serverEnd : undefined,
                        Date.now(),
                        false,
                        url,
                        "GET",
                        undefined,
                        params,
                        response.status,
                        undefined
                    );
                }

                if (responseCallBack) {
                    responseCallBack(response);
                }
            })
        .catch((error) => {
                if(jsbaLogger){
                    jsbaLogger.logHttpRequest(logName,
                        clientStart,
                        undefined,
                        undefined,
                        Date.now(),
                        false,
                        url,
                        "GET",
                        undefined,
                        params,
                        "500",
                        error.toString()
                    );
                }
                if (axios.isCancel(error)) {
                console.log('Request canceled');
            }else if(errorCallBack) {
                errorCallBack(error);
            }
        });
            return source;
        },
        post(url, params, headers, jsbaLogger, logName, responseCallBack, errorCallBack){
            var clientStart = Date.now();
            var CancelToken = axios.CancelToken;
            var source = CancelToken.source();
            if (params == null) {
                params = {};
            }
            params.cancelToken = source.token;

            if(headers==null){
                headers = {};
            }

            axios.post(url, params, headers,timeout)
                .then((response) => {
                if(jsbaLogger){
                    jsbaLogger.logHttpRequest(logName,
                        clientStart,
                        response.data.time ? response.data.time.serverStart : undefined,
                        response.data.time ? response.data.time.serverEnd : undefined,
                        Date.now(),
                        false,
                        url,
                        "POST",
                        undefined,
                        params,
                        response.status,
                        undefined
                    );
                }

                if (responseCallBack) {
                    responseCallBack(response);
                }
            })
        .catch((error) => {
                if(jsbaLogger){
                    jsbaLogger.logHttpRequest(logName,
                        clientStart,
                        undefined,
                        undefined,
                        Date.now(),
                        false,
                        url,
                        "POST",
                        undefined,
                        params,
                        "500",
                        error.toString()
                    );
                }
                if (axios.isCancel(error)) {
                console.log('Request canceled');
            }else if(errorCallBack) {
                errorCallBack(error);
            }
        });
            return source;
        },

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
penance316commented, Jan 22, 2018

it seems for POST requests the cancellation token needs to be an axios parameter and not part of the POST data.

axios({
    method:      'post',
    url:         'https://jsonplaceholder.typicode.com/posts/1',
    data:        {
    	    something:     'somedata string',
	    reference:     'maybe another parameter'
    },
    cancelToken: source.token,
})

This now allows me to cancel a POST request. it seems like something that should be mentioned in the documentation.

0reactions
leokyeungcommented, Dec 14, 2019

it seems for POST requests the cancellation token needs to be an axios parameter and not part of the POST data.

axios({
    method:      'post',
    url:         'https://jsonplaceholder.typicode.com/posts/1',
    data:        {
    	    something:     'somedata string',
	    reference:     'maybe another parameter'
    },
    cancelToken: source.token,
})

This now allows me to cancel a POST request. it seems like something that should be mentioned in the documentation.

Thanks for your help!!!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Not working on cancellation of POST in Axios - Stack Overflow
I faced the same issue with a get request. You need to pass headers in axios properties. axios.get(url, { cancelToken: ..., headers: {...
Read more >
How to Perform HTTP Requests with Axios – A Complete Guide
Sending asynchronous HTTP queries to REST endpoints and performing CRUD operations is simple using Axios POST request and GET request.
Read more >
Cancellation in coroutines - Medium
Calling job1.cancel ensures that only that specific coroutine gets cancelled and all the other siblings are not affected:
Read more >
Direct Pay Help | Internal Revenue Service
Get answers to common questions about making payments with Direct ... Does Direct Pay provide confirmation my payment request was submitted?
Read more >
Cancel Google Workspace - Google Workspace Admin Help
If your users purchased additional storage, it's no longer available. Shared drives—After you cancel, you can view shared drive data, but you can't...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found