Function url::get()

← Back to Class url



Module urlClass url → get()

Description

Perform HTTP GET request

url::get(url, request_params = "", headers = nothing, downloaded_to = "", progress_callback = nothing)


Parameters


Return Value


Usage Example

import url
 
 
' Simple usage example
 
response = url.get("https://example.com/")
 
if response.code == http.ok
    writeln(response.body)                    ' Success
elseif response.code != 0
    writeln("Error: HTTP " & response.code)   ' HTTP error
else
    writeln(response.error_string)            ' Connection or other error
endif
 
 
' HTTP GET with headers and params
 
headers = {
    "Authorization": "Basic QWxhZGRpbjpPcGVuU2VzYW1l",
    "User-Agent": "myapp/1.0.0"
}
 
params = {
    "id": "A0110",
    "name": "Sarah Zaira"
}
 
response = url.get("https://faruq.id/misc/get.php", params, headers)
 
if response.code == 200
    writeln(response.body)
elseif response.code != 0
    writeln("Error: HTTP " & response.code)
else
    writeln(response.error_string)
endif
 
 
' Simple example how to download a file
 
response = url.get("https://dinfio.org/releases/files/3.1.03/dinfio-3.1.03-macos.pkg", "", {}, "file.pkg")
 
if response.code == 200
    writeln("Download complete!")
else
    writeln(response.error_string)
endif
 
 
' Downloading a file with progress callback
 
response = url.get("https://dinfio.org/releases/files/3.1.03/dinfio-3.1.03-macos.pkg", "", {}, "file.pkg", progress())
 
if response.code == 200
    writeln("Download complete!")
else
    writeln(response.error_string)
endif
 
function progress(downloaded, total_download, uploaded, total_upload)
    write("Downloading... " & bytes_unit(downloaded) & " of " & bytes_unit(total_download))
 
    return 0   ' Note: 0 to continue downloading/uploading progress
               '       1 to stop the progress
stop