network.cancel()

Type Function
Library network.*
Return value none
Revision Release 2022.3683
Keywords asynchronous, http, https, get, post
See also network.request()
network.download()
networkRequest

Overview

Cancel an outstanding network request made with network.request(), network.upload(), or network.download().

Syntax

network.cancel( requestId )
requestId (required)

Userdata. The request handle provided by network.request(), network.upload(), or network.download(). This handle is returned by each of these functions and is also provided in the networkRequest event.

Examples

Cancel Request Based on User Input
local function networkListener( event )

    if ( event.isError ) then
        print( "Network error: ", event.response )
    else
        print( "Request complete" )
    end
end
 
-- Start the request:
local requestId = network.request( "https://encrypted.google.com", "GET", networkListener )

-- Create a cancel button that can cancel the request:
local cancelButton = display.newImage( "cancelButton.png" )

function cancelButton:tap( event )
    print( "Canceling request via cancel button" )
    network.cancel( requestId )
end

cancelButton:addEventListener( "tap", cancelButton )
Cancel Request Inside of Progress Listener
-- The following sample code starts an image download and, in the initial progress
-- notification, determines whether or not to continue based on the file's size.

local function networkListener( event )

    if ( event.isError ) then
        print( "Network error: ", event.response )
    elseif ( event.phase == "began" ) and ( event.bytesEstimated > 80000 ) then
        print( "Canceling request, file is too big!" )
        network.cancel( event.requestId )
    end
end

-- Start the image download
network.download(
    "https://plugins.solar2d.com/images/logo-banner.png",
    "GET",
    networkListener,
    { progress = true },
    "bannerCopy.png",
    system.TemporaryDirectory )