Godot: HTTPRequest see downloading progress.

Created on 28 May 2019  路  5Comments  路  Source: godotengine/godot

Hello,

Is there a way to see the progress while downloading a file using HTTPRequest? If not then I think it should be added. Also as a newcomer, I have to say that the docs are VERY confusing and often it is not clear what class to use ( HTTPClient, HTTPRequest in this case). As it is expected I went for HTTPRequest as it seems to be the easiest to use but it seems to be lacking functionality. Also, most of the tutorials out there are outdated which makes it even harder for anyone who's just starting to do any progress. I will be submitting a few ideas on how to make the engine more accessible to newcomers.
Thank you

archived discussion enhancement feature proposal network

Most helpful comment

For anyone who is also interested in the future about this, you can see the progress of a download with HTTPRequest by using get_downloaded_bytes().

var bodySize = get_body_size()
var downloadedBytes = get_downloaded_bytes()

var percent = int(downloadedBytes*100/bodySize)
print(str(percent) + " downloaded")

All 5 comments

This is possible with the HTTPClient

This is possible with the HTTPClient

Thank you @LinuxUserGD , I know that already but I had to go through a lot of trouble to find it out as I state in my post.

Question:
Is HTTPRequest some kind of custom node? is it written in GDScript ( since it is built on top of HTTPClient it would make sense )? Can someone please guide me on how I could contribute to the code and possibly add download progress as discussed above ( edit the code of HTTPRequest somehow and push the changes here)?

Here is an example (don't know the source anymore, it was in some forum) which is converted from 2 to 3:
main.gd included in http.tscn

extends Node
var t = Thread.new()
func _init():
 var arg_bytes_loaded = {"name":"bytes_loaded","type":TYPE_INT}
 var arg_bytes_total = {"name":"bytes_total","type":TYPE_INT}
 add_user_signal("loading",[arg_bytes_loaded,arg_bytes_total])
 var arg_result = {"name":"result","type":TYPE_RAW_ARRAY}
 add_user_signal("loaded",[arg_result])
 pass
func getting(domain,url,port,ssl):
 if(t.is_active()):
  return
 t.start(self,"_load",{"domain":domain,"url":url,"port":port,"ssl":ssl})

func _load(params):
 var err = 0
 var http = HTTPClient.new()
 err = http.connect_to_host(params.domain,params.port,params.ssl)  #-- NOTE: Automatically converted by Godot 2 to 3 converter, please review

 while(http.get_status() == HTTPClient.STATUS_CONNECTING or http.get_status() == HTTPClient.STATUS_RESOLVING):
  http.poll()
  OS.delay_msec(100)

 var headers = [
  "User-Agent: Pirulo/1.0 (Godot)",
  "Accept: */*"
 ]

 err = http.request(HTTPClient.METHOD_GET,params.url,headers)

 while (http.get_status() == HTTPClient.STATUS_REQUESTING):
  http.poll()
  OS.delay_msec(500)
# 
 var rb = PoolByteArray()  #-- NOTE: Automatically converted by Godot 2 to 3 converter, please review
 if(http.has_response()):
  var new_headers = http.get_response_headers_as_dictionary()
  while(http.get_status()==HTTPClient.STATUS_BODY):
   http.poll()
   var chunk = http.read_response_body_chunk()
   if(chunk.size()==0):
    OS.delay_usec(100)
   else:
    rb = rb+chunk
    call_deferred("_send_loading_signal",rb.size(),http.get_response_body_length())

 call_deferred("_send_loaded_signal")
 http.close()
 return rb
func _send_loading_signal(l,t):
 emit_signal("loading",l,t)
 pass

func _send_loaded_signal():
 var r = t.wait_to_finish()
 emit_signal("loaded",r)
 pass

UpdateManager.gd included in UpdateManager.tscn which calls main.gd (autoload)

extends Node
var check = false
var files = 0
var array = []
var file=""

func _on_loading(loaded,total): 
    var percent
    if total != 0:
        percent = int(loaded*100/total)
    get_node("ProgressBar").set_value(percent)

func _on_loaded(result):
    var filename = file.split("/")
    var Data = File.new()
    Data.open("user://" + filename[filename.size()-1], File.WRITE)
    Data.store_buffer(result)
    if check == false:
        next()
    else:
        Update(result)

func Download(domain, address, name):
    file = name
    var filename = name.split("/")
    var directory = Directory.new();
    var FileExists = directory.file_exists("user://" + filename[filename.size()-1])
    if check == true or not FileExists:
        file = name
        get_node("ProgressBar").set_value(0)
        var http = load("res://http.tscn").instance()
        http.connect("loading",self,"_on_loading")
        http.connect("loaded",self,"_on_loaded")
        http.getting(domain,address + name,443, true) #domain,url,port,useSSL
    else:
        next()
func Update(res):
    var string = res.get_string_from_utf8()
    print(string)
    string = string.replace(" ", "")
    string = string.replace("[", "")
    string = string.replace("]", "")
    array = string.split(",")
    check = false
    next()

func next():
    if files < array.size():
        files += 1
        get_node("ProgressBar/number").set_text(str(files) + " of " + str(array.size()))
        get_node("ProgressBar/filename").set_text(str(array[files-1]))
        Download("https://cdn.pixabay.com","/photo/", array[files-1])
    else:
        get_node("ProgressBar/number").set_text("done")
        get_node("ProgressBar/filename").set_text("")



func _ready():
    check = true
    Download("https://raw.githubusercontent.com","/HugeGameArtGD/Solus-Stunts/master/", "index.txt")

http.tscn

[gd_scene load_steps=2 format=2]

[ext_resource path="res://main.gd" type="Script" id=1]

[node name="Node" type="Node" index="0"]

script = ExtResource( 1 )

UpdateManager.tscn

[gd_scene load_steps=2 format=2]

[ext_resource path="res://UpdateManager.gd" type="Script" id=1]

[node name="UpdateManager" type="Node"]
script = ExtResource( 1 )

[node name="ProgressBar" type="ProgressBar" parent="."]
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
margin_left = -199.0
margin_top = -13.5
margin_right = 199.0
margin_bottom = 13.5

[node name="number" type="Label" parent="ProgressBar"]
anchor_top = 0.5
anchor_bottom = 0.5
margin_top = 16.5
margin_right = 136.0
margin_bottom = 50.5
size_flags_vertical = 0

[node name="filename" type="Label" parent="ProgressBar"]
anchor_left = 1.0
anchor_top = 0.5
anchor_right = 1.0
anchor_bottom = 0.5
margin_left = -237.0
margin_top = 16.5
margin_bottom = 50.5
size_flags_vertical = 0
align = 2

@zaksnet (this is for the HTTPClient)

For anyone who is also interested in the future about this, you can see the progress of a download with HTTPRequest by using get_downloaded_bytes().

var bodySize = get_body_size()
var downloadedBytes = get_downloaded_bytes()

var percent = int(downloadedBytes*100/bodySize)
print(str(percent) + " downloaded")
Was this page helpful?
0 / 5 - 0 ratings

Related issues

timoschwarzer picture timoschwarzer  路  3Comments

testman42 picture testman42  路  3Comments

n-pigeon picture n-pigeon  路  3Comments

nunodonato picture nunodonato  路  3Comments

Zylann picture Zylann  路  3Comments