简体   繁体   中英

How to Cancel a REST api request?

I use REST API based system, in which there are some requests that take long time to complete. I want to give user an option to cancel the request.

First, support

POST /requests

which will return a reference to the status of the request

{
    "id": 1234,
    "self"": "/requests/1234"
    "status": "Running"
}

Then add support for

PUT /requests/1234
{
    "status": "Canceled:"
}

That will let clients cancel a request if it hasn't finished yet. If the request is to create some other kind of resource, then instead of POST /requests , do POST /myResource , but still return the status object with the pointer to /requests in the response.

Clients can then poll /requests to see when the request is complete.

Firstly you need to use multiple threads , because your program will be on hold while it is sending the request so you cannot click on something until it is back from hold.

Create a thread which calls the rest API in background without hanging the over all application and terminate that thread on click of a button.

note for terminating the thread you need to use stop function which is depreciated now , because you cannot interrupt the thread or check a Boolean during the process.

       @Deprecated
       public final void stop()

Alternatively you can use Maximum Time for a HTTP Request call by

    HttpConnectionParams.setConnectionTimeout(httpParams, 30000);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM