0 votes

Hi, I am trying to implement something similar to this but in Godot.
It just sends player responses to a google form.

https://www.youtube.com/watch?v=z9b5aRfrz7M
https://github.com/luzan/Unity-Google-Spreadsheet/blob/master/Assets/SendToGoogle.cs

I've managed to connect with the web site and make the request. But for some reason it is not submiting the respose.

This is what I've got so far:

func _send_data():
    var err = 0
    var http = HTTPClient.new() # Create the Client.

    err = http.connect_to_host("docs.google.com", 80) # Connect to host/port.
    assert(err == OK) # Make sure connection was OK.
    print (err)

    # Wait until resolved and connected.
    while http.get_status() == HTTPClient.STATUS_CONNECTING or http.get_status() == HTTPClient.STATUS_RESOLVING:
        http.poll()
        print("Connecting...")
        OS.delay_msec(500)
    print (http.get_status())
    assert(http.get_status() == HTTPClient.STATUS_CONNECTED) # Could not connect  

    var header = ["entry.986078935", "Content-Type: application/json"]
    var field = JSON.print(str(user_melody))
    var result = http.request(http.METHOD_POST, "/forms/d/e/1FAIpQLSe8Z91NGAN-ssDJbMSOuZtM7eWZQvt8mWoMxuJ79HtM-f4Qkg/formResponse", header, field)
    print(result)

    # Keep polling until the request is going on
    while (http.get_status() == HTTPClient.STATUS_REQUESTING):
        http.poll()
        print("Requesting...")
        OS.delay_msec(300)
    # Make sure request finished
    print (http.get_status())
    assert(http.get_status() == HTTPClient.STATUS_BODY or http.get_status() == HTTPClient.STATUS_CONNECTED)

The debug Output prints:

0
Connecting...
Connecting...
5
0
Requesting...
Requesting...
7

Not sure what is going on... If you have any clue, please let me know!

asked Oct 25, 2019 in Engine by Mentuhetep (24 points)

1 Answer

0 votes
Best answer

I figured it out!
I am sharing the answer in case someone needs it.
I created a HTTPRequest Node and added a script there with this code:

extends HTTPRequest

var my_full_url = "https://docs.google.com/forms/d/e/****INSERT_YOUR_FORM_URL_HERE****/formResponse"
    var my_data = {"entry.###WHATEVER YOUR ENTRY KEY IS###" : "***ADD YOUR RESPONSE HERE***"}
    var headers = ["Content-Type: application/x-www-form-urlencoded"]
    var http = HTTPClient.new()

    func _ready() -> void:
        send_data()

    func send_data():
        # Transform data so it can be sent:
        var headers_pool = PoolStringArray(headers)
        var my_data_ready = http.query_string_from_dict(my_data)
        #Send data!!!!
        var result = self.request(my_full_url, headers_pool, false, http.METHOD_POST, my_data_ready)
answered Oct 26, 2019 by Mentuhetep (24 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.