1. Hey! Guest! The 35th GMC Jam will take place between November 28th, 12:00 UTC - December 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

Help with downloading a file using http_get_file

Discussion in 'Programming' started by LDinos, Nov 8, 2019.

  1. LDinos

    LDinos Member

    Joined:
    Mar 18, 2018
    Posts:
    13
    Greetings! I am trying to make a simple auto update checker, and I thought of doing it like this: Download a text file that I have uploaded, read the string inside it, and compare it to the current version string. However I have a problem using http_get_file. Here's the object

    Create event:
    Code:
    file = http_get_file("https://drive.google.com/uc?export=download&id=18TOKTHNzruc92BoK6YGOiQy1rLEqhSAi", working_directory)
    The link is a direct download

    HTTP event:
    Code:
    if ds_map_find_value(async_load, "id") == file
        {
        var status = ds_map_find_value(async_load, "status");
        var full_status = ds_map_find_value(async_load,"http_status");
        if status == 0
            {
            var path = ds_map_find_value(async_load, "result");
            var txt = file_text_open_read(path)
                var ver = file_text_read_string(txt)
                show_message(ver + " - " + global.version)
            file_text_close(txt)
            }
        else {str = full_status} //show me the error if status = -1
        }
    What happens: Status returns -1 and full_status returns 200! The file is not even downloaded :(
    What am I doing wrong?

    Using GMS 2.2.4.474
     
  2. LDinos

    LDinos Member

    Joined:
    Mar 18, 2018
    Posts:
    13
    I fixed it by using "http_get" instead!
     
  3. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,607
    Did you read the example at the bottom of http_get_file()'s Manual entry? The second argument to http_get_file() expects a complete file name, not just a directory name. The HTTP result is 200 because your URL is valid, but the status is -1 because you are trying to overwrite working_directory with the file you downloaded.

    This is what you should have done:
    Code:
    file = http_get_file("https://drive.google.com/uc?export=download&id=18TOKTHNzruc92BoK6YGOiQy1rLEqhSAi", working_directory + "vercheck.txt");
    
    http_get() is not a complete replacement for http_get_file(). It may work for you at the moment, but the http_get() approach will come apart the instant you download any binary-formatted file that has a 0 byte in it (which terminates strings early). You should learn the right way with http_get_file() going forward.
     
  4. LDinos

    LDinos Member

    Joined:
    Mar 18, 2018
    Posts:
    13
    Ah, thank you very much, this makes sense
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice