0

I have started with the minehut API, and after looking at the docs (see a copy here) it uses get and post. As a newbie to javascript etc I dont know how it works.


Part 1 - Get Info

for example: I want to get info about a server, it says to use GET https://api.minehut.com/server/{server-id} How would i get for example playercount from it so that i can give that info to my code and display it on my website. Required headers: is also mentioned in the docs, what are these and how do i use them?

Part 2 - send info

Now say for example i want to run a command, the docs say to use POST /server/{Server ID}/send_command. It also mentions Required headers saying it needs Content-Type, Authorization and x-session-id how would i send a string so that it would use POST to run a command

  • I think you are trying to call a API from using ajax. For that you can use jQuery or simple JavaScript. By default Header have content-type of json which could work for you. Here is a link to learn how to make a ajax call https://www.w3schools.com/jquery/ajax_ajax.asp – Ali Khan Oct 09 '19 at 12:10
  • it returns `ReferenceError: $ is not defined` when using `var info = $.ajax({` then without the `$` it says `TypeError: ajax is not a function` @AliKhan – jontus technology Oct 09 '19 at 12:31
  • have you included jquery library in your code? this is happening because you have not included jquery in your project. You can include that with a direct cdn and also can download the files and include then in your project. you can find documentation here https://www.w3schools.com/jquery/jquery_get_started.asp – Ali Khan Oct 09 '19 at 12:38
  • So is there a way that i can then add this to some code in node.js code? as i have a discord bot that runs from node.js that i will use to trigger the get() @AliKhan – jontus technology Oct 09 '19 at 12:59
  • jQuery is related to front end so i cannot be used with node js but there are ways to generate a API call in simple javascript. Fr refrence plese see this answer https://stackoverflow.com/questions/36975619/how-to-call-a-rest-web-service-api-from-javascript – Ali Khan Oct 09 '19 at 13:20
  • you are welcome. please up vote comments if you find them useful. – Ali Khan Oct 09 '19 at 14:06

1 Answers1

0

What you need to send or receive data using GET/POST methods is XMLHttpRequest object.

var server_id = "EXAMPLE_ID";
var req = new XMLHttpRequest();
req.onreadystatechange = function(){
    if(this.readyState == 4){
        // Data sent back available in this.responseText
        // For example:
        var recData = this.responseText;
        // further handling
    }
}
req.open('GET', 'https://api.minehut.com/server/' + server_id, true);
req.send();

Or in case of POST request:

req.open('POST', 'https://api.minehut.com/server/' + server_id + '/send_command', true);
req.setRequestHeader("Content-Type", "pplication/x-www-form-urlencoded");
req.setRequestHeader("Authorization", "Basic " + btoa(user + ":" + pass));
req.setRequestHeader("HeaderNameExample", "ItsValueExample");
req.send('optionalVar=sentData&foo=bar&etc");

In some cases preflight request can be done (especially with custom request headers) and the request might fail. To avoid that you might try invoking the opening with user/password instead. For cross domain request Access-Controll request should be made which allows for cookies to be set.

req.open("GET", url, true, username, password);

req.open("POST", url, true, username, password);

req.withCredentials = true;
Joriom
  • 16
  • 3