HTTPRequest won't work?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By P0werman1

In the documentation for HTTPRequest, there is the line:

error = http_request.request("https://httpbin.org/post", [], true, HTTPClient.METHOD_POST, body)

I have:
http_request.request(url, header, true, method, request_body) var response = await http_request.request_completed

And it returns the errors:
Too many arguments for "request()" call. Expected at most 4 but received 5. Cannot pass a value of type "bool" as "HTTPClient.Method". Invalid argument for "request()" function: argument 3 should be "HTTPClient.Method" but is "bool".

I don’t understand why that doesn’t work, can someone please help me?

:bust_in_silhouette: Reply From: jgodfrey

Looks like that part of the sample code is a carry-over from the Godot 3.5, where the request function did accept up to 5 arguments. If you look at the Godot 4.0 function docs, you’ll see it only expects up to 4 arguments (as the error states).

Seems like you need to remove that 3rd (true) arg.

Docs here

If I remove the 3rd argument, does Godot 4.0 treat is as Godot 3.x would treat it with “true”?

P0werman1 | 2023-05-03 17:17

I assume yes, but really, I don’t know for sure. I’ve done a bit of poking around and, so far, haven’t found any mention of the arg’s removal or what the new expectations are.

jgodfrey | 2023-05-03 17:48

Thank you for the help, I’m trying to see what I can find on my own. Sadly, there isn’t a huge community for this, and I can’t see examples of what others have done in Godot 4.0. I can only find a small number from Godot 3.x.

P0werman1 | 2023-05-03 17:56

I can’t see examples of what others have done in Godot 4.0

Not sure what you mean here. For Godot 4, they obviously haven’t supplied the (now removed) ssl_validate_domain arg to request().

jgodfrey | 2023-05-03 18:01

I mean that, with larger game engines, I can find 100s of tutorials online for everything I could need. With Godot, I’m stuck figuring it out mostly for myself.

P0werman1 | 2023-05-03 18:03

Ah, understood. Certainly, the community is smaller than some, but growing fast. In case you’re unaware, there are lots of public Godot projects on Github. So, doing a Github-wide search similar to:

httprequest language:GDScript

…might lead you to some interesting results, though you’ll have to weed through lots of uninteresting stuff too. And, you’ll have the additional complication of the function signature change between Godot 4 and Godot 3.

jgodfrey | 2023-05-03 18:08

I found one thing in C#, and 4 more from Godot 3.x. There was nothing else. Interesting idea though, will definitely steal it in the future.

I updated my code to look like this:

var http_request = HTTPRequest.new()

add_child(http_request)
http_request.request(url, header, method, request_body)
var response = await http_request.request_completed
var json = JSON.new()
json.parse(response[3].get_string_from_utf8())

And for some reason, json is just equal to <JSON#-9223371994307754837>. Any idea why? It should look something like this:

{
  "id": 406,
  "game_id": 2,
  "email": "test@lootlocker.io",
  "created_at": "2021-08-25T08:03:34.612346459Z",
  "updated_at": "2021-08-25T08:03:34.612346459Z",
  "deleted_at": null,
  "validated_at": null
}

P0werman1 | 2023-05-03 18:18

Edit to fix code formatting.

Tip - select the pasted code, press the { } button, verify the result in the preview panel before submitting.

jgodfrey | 2023-05-03 22:40

Regarding the JSON issue…

I assume the JSON representation you mention above is just the JSON object itself. Are you looking for the parsed JSON text? If so, you can get that via get_parsed_text(), assuming you told the parse() call to keep it (which you have not). You need to pass in true as a 2nd arg to parse().

Or, maybe you’re after the data member after parsing. Or, the stringifyed version of the data. All of that’s do’able.

See the details in the docs

jgodfrey | 2023-05-03 22:48

That made very little sense to me, but I’ll experiment with what you’ve said and hope I can get something. I did understand what you meant by it being the JSON object itself though, so I can figure out how to deal with it from there.

EDIT: I got it working, thank you so much for your help!

P0werman1 | 2023-05-04 14:16