#Cannot send HttpRequest To Webhook

1 messages · Page 1 of 1 (latest)

tall hatch
#

Not sure whats wrong

@tool
extends Button

var http_req_node: HTTPRequest
var url = read_file("res://addons/game_metrics/secret.txt").strip_escapes()

func read_file(path):
    var file = FileAccess.open(path, FileAccess.READ)
    var content = file.get_as_text()
    return content

func dict_to_packed_string_arr(dict:Dictionary):
    var l = []
    for key in dict.keys():
        var value = dict[key]
        l.append([key, value])
    print(l)
    return PackedStringArray(l)

func _enter_tree():
    http_req_node = HTTPRequest.new()
    add_child(http_req_node)
    http_req_node.request_completed.connect(self._request_completed)
    print(http_req_node)
    
    pressed.connect(clicked)

func _request_completed(result, response_code, headers,body):
    var json = JSON.new()
    json.parse(body.get_string_from_utf8())
    var response = json.data
    
    print(response.headers["User-Agent"])

func clicked():
    print("You clicked me!")
    
    var headers = dict_to_packed_string_arr({"Content-Type": "application/json"})
    print(headers)
    var data = JSON.new().stringify({"content":"Hello from godot!"})
    print("data: ",data)
    
    var error = http_req_node.request(url, headers, HTTPClient.METHOD_POST, data)
    if error != OK:
        push_error("An error occurred in the HTTP request.")

#

my secret.txt contains my discord webhook, I tested the same webhook with python, its valid

#

but in godot I cant make it send it

#

this script belongs to a button that runs the clicked() function when I click it

#

Cannot send HttpRequest To Webhook