HTTP Request Issue

Greeb

Member
Hello,

I have written an API for submitting highscores to a database using Node.js and I have deployed it to the Google App Engine. The API recieves commands through HTTP requests and acts accordingly based on the information contained in the request. When testing the API locally on my machine, it worked perfectly, however once I deployed the API to Google App Engine, the HTTP requests I am sending to API are not being acknowledged by it. I understand that there could be a multitude of issues occuring here, however I have been able to rule out that my App Engine service is set up correctly because I am able to interact with it as expected through the use of Postman. The following code is what I am using to create and send the request within GMS:2

GML:
function submit_highscore(){

  

    // Header   

    var header = ds_map_create()

    ds_map_add(header, "Host", "https://tetragun.uc.r.appspot.com/SubmitHighscore")

    ds_map_add(header, "Content-Type", "application/json")



    // Content

    var body = ds_map_create()

    ds_map_add(body, "score", score)

    ds_map_add(body, "wave", global.wave)

    ds_map_add(body, "loop", global.loop)

    ds_map_add(body, "username", "User")

    ds_map_add(body, "turret_type", 1)

    player_mods_array[0] = 0

    player_mods_tier_array[0] = 0

    for(var i = 0; i < ds_list_size(global.activated_player_mods); i++){

        var current_mod = ds_list_find_value(global.activated_player_mods, i)

        player_mods_array[i] = current_mod.mod_number

        player_mods_tier_array[i] = current_mod.tier

    }

    enemy_mods_array[0] = 0

    ds_map_add(body, "player_mods", player_mods_array)

    ds_map_add(body, "player_mod_tiers", player_mods_tier_array)

    for(var i = 0; i < ds_list_size(global.activated_enemy_mods); i++){

        var current_mod = ds_list_find_value(global.activated_enemy_mods, i)

        enemy_mods_array[i] = current_mod.mod_number

    }

    ds_map_add(body, "enemy_mods", enemy_mods_array)

    var date_string = generate_date_time_string()

    ds_map_add(body, "submission_timestamp", date_string)

    ds_map_add(body, "key", obtain_password(date_string))

    body = json_encode(body)

  

    // Send

    http_request("https://tetragun.uc.r.appspot.com/SubmitHighscore", "POST", header, body)

    show_debug_message("Request Sent")

  

}
In my API, I am using Express to handle the incoming requests. Here is the code the API is using to recieve and handle the requests:


JavaScript:
const express = require("express")

const app = express()

const port = process.env.PORT

const bodyParser = require("body-parser")

let jsonParser = bodyParser.json()



// Initialize Cloud Firestore

const admin = require('firebase-admin');

const serviceAccount = require('./service-account-details.json');

admin.initializeApp({

    credential: admin.credential.cert(serviceAccount)

});

const db = admin.firestore();





// ----------------- SUBMIT HIGH SCORE ----------------- \\



app.post('/SubmitHighscore', jsonParser, (req, res) => SubmitHighscore(req, res));



async function SubmitHighscore(req, res) {



    if (!validateRequest(req.body)) {

        console.log("Validation Failed")

        res.send("failure")

        return

    }



    let newRef = await db.collection('highscores').add({

        username: req.body.username,

        score: req.body.score,

        wave: req.body.wave,

        loop: req.body.loop,

        turret_type: req.body.turret_type,

        player_mods: req.body.player_mods,

        player_mod_tiers: req.body.player_mod_tiers,

        enemy_mods: req.body.enemy_mods,

        submission_timestamp: req.body.submission_timestamp

    });



    console.log("Client High Score Object: " + JSON.stringify(req.body) + "\n\nDocument ID: " + newRef.id)



    res.send("success")



}

I suspect the issue lies withing the GML http_request function and it adding some headers that perhaps App Engine doesn't like, though I am not sure. If anybody has worked with something similar or has any hunch as to what is going on, your input would be much appreciated.
 

FrostyCat

Member
Remove this line:
GML:
ds_map_add(header, "Host", "https://tetragun.uc.r.appspot.com/SubmitHighscore")
http_request already populates the Host header with the correct value by default, and you ruined it by overwriting it with something completely invalid. Read up on what the Host header actually does before trying to be clever with it.
 

Greeb

Member
Remove this line:
GML:
ds_map_add(header, "Host", "https://tetragun.uc.r.appspot.com/SubmitHighscore")
http_request already populates the Host header with the correct value by default, and you ruined it by overwriting it with something completely invalid. Read up on what the Host header actually does before trying to be clever with it.
Got it. No more cleverness from me 👍 Does the same go for the content type? Do I need to explicitly declare that it’s JSON?

Edit: Your advice worked. Thank you very much for the assistance!
 
Last edited:
Top