Godot HTTPRequest not working for my API in HTML5

handisbhandisb Posts: 2Unconfirmed Member
edited July 27 in Exporting

Hello!

I am trying to implement the HTTPRequest node in Godot to call an API I have created. I have a project that is exported to HTML5 and is supposed to send an HTTPRequest to http://localhost:8000/2/tokens/update when the game finishes. However, the request never seems to send, despite the fact that the request() function is not returning any errors and the url it is using seems to be correct.

Here is the code in the HTTPRequest node:

func giveTokens(url, amount):

var full_url = str(url)+'?amount='+str(amount)

# Convert data to json string:

var query = JSON.print({'amount': amount})

# Add 'Content-Type' header:

var headers = ["Content-Type: text/html; charset=UTF-8", "Access-Control-Allow-Credentials: true", "Access-Control-Allow-Origin: *"]

var error = self.request(full_url, headers, false, HTTPClient.METHOD_PUT, query)

JavaScript.eval("console.log("+str(error)+");")

JavaScript.eval("console.log('"+str(full_url)+"');")

And here is the code that calls it:

func _on_GameTimer_timeout():

$GameOver.score = score

$`[`GameOver.show`](https://GameOver.show)`()

var url = "`[`http://localhost:8000/2/tokens/update`](http://localhost:8000/2/tokens/update)`"

#if OS.get_name()=="HTML5":

#   url = JavaScript.eval("""url""")

$HTTPRequest.giveTokens(url, score)

get_tree().paused = true

I know that _on_GameTimer_timeout() works because the game pauses when the game timer goes to 0. In addition, I also know that the API I am using works properly because I can use it perfectly with Postman. The API is hosted on the same hostname (localhost:8000) and I am using Firefox on Windows.

I am completely stumped on what to do. Any help would be greatly appreciated!

Answers

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file