• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Getting a response using a payload

607

Member
Hello, I've been looking at the Mojang API.
I want to get the UUID of a certain playername. I've found this:

I am however not acquainted with this 'POST' thing, and payloads. I've done some more Googling, and found someone explaining how it works in PHP. I have never used PHP, but I do think I can understand what's going on here.
However, I'm using GameMaker Studio, not PHP. Can I still use this method? As I said, I am not acquainted with this, so I don't really know anything else than what I've found through Googling and common sense. :) I would however like to learn about it!
There are plenty of sites online to convert UUID's to playernames, but I think I should be able to do it directly through the API, as I would learn more from that, and not be dependent on third-party websites. I would appreciate your help. :)
 

FrostyCat

Redemption Seeker
Of course you can, you just need to read up on how to use http_request() and follow the API's instructions.
Code:
var headers = ds_map_create();
ds_map_add(headers, "Content-Type", "application/json");
ah_request = http_request("https://api.mojang.com/profiles/minecraft", "POST", headers, '["someplayer"]');
ds_map_destroy(headers);
And an example of how to handle the response in the HTTP event:
Code:
if (async_load[? "id"] == ah_request) {
  switch (async_load[? "status"]) {
    case 1: break;
    case 0:
      var result = async_load[? "result"],
          json = json_decode(result),
          target = json;
      target = target[? "default"];
      if (ds_list_empty(target)) {
        show_debug_message("Player does not exist.");
      }
      else {
        target = target[| 0];
        show_debug_message(target[? "name"] + ": " + target[? "id"]);
      }
      ds_map_destroy(json);
    break;
    default:
      show_debug_message("Request failed");
    break;
  }
}
 
  • Like
Reactions: 607

YellowAfterlife

ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ
Forum Staff
Moderator
Making a request:
Code:
var req = ds_map_create();
http_request("https://api.mojang.com/profiles/minecraft", "POST", req, '["YellowAfterlife"]');
ds_map_destroy(req);
And in HTTP event:
Code:
show_debug_message(json_encode(async_load)); // display event data for debug purposes
For requests that take a JS Object { ... }, you can use json_encode to pass it as the last argument.
... for the rest, see a swifter reply above.
 
  • Like
Reactions: 607

607

Member
Of course you can, you just need to read up on how to use http_request() and follow the API's instructions.
Code:
var headers = ds_map_create();
ds_map_add(headers, "Content-Type", "application/json");
ah_request = http_request("https://api.mojang.com/profiles/minecraft", "POST", headers, '["someplayer"]');
ds_map_destroy(headers);
And an example of how to handle the response in the HTTP event:
Code:
if (async_load[? "id"] == ah_request) {
  switch (async_load[? "status"]) {
    case 1: break;
    case 0:
      var result = async_load[? "result"],
          json = json_decode(result),
          target = json;
      target = target[? "default"];
      if (ds_list_empty(target)) {
        show_debug_message("Player does not exist.");
      }
      else {
        target = target[| 0];
        show_debug_message(target[? "name"] + ": " + target[? "id"]);
      }
      ds_map_destroy(json);
    break;
    default:
      show_debug_message("Request failed");
    break;
  }
}
I get this error:
Code:
Data structure with index does not exist.
 at gml_Object_object0_Key_ENTER_1 (line 10) - if (async_load[? "id"] == ah_request) {
 

FrostyCat

Redemption Seeker
Why is it that every time I post about HTTP requests, someone always disregards my instructions about using the HTTP event?
And an example of how to handle the response in the HTTP event:
I did not tell you to stick that piece of code immediately after the http_request() call, I told you to stick it in the HTTP event separately.

This can't possibly be that hard, could it?
 
  • Like
Reactions: 607

607

Member
Why is it that every time I post about HTTP requests, someone always disregards my instructions about using the HTTP event?

I did not tell you to stick that piece of code immediately after the http_request() call, I told you to stick it in the HTTP event separately.

This can't possibly be that hard, could it?
Ugh, that's a stupid mistake, I'm sorry.
I'll read into when to use the HTTP event.
 
Top