You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

新手技术问询:如何使用GET/POST方法与Minehut API交互

Working with the Minehut API in JavaScript for Beginners

Hey there! Let's walk through solving your two questions step by step—since you're new to JavaScript, I'll keep things straightforward and explain every part so you understand what's going on.

Question 1: Fetch Server Information (GET Request)

First, let's tackle how to get your server's player count and display it on your site, plus break down what "Required headers" mean.

What are Required Headers?

Required headers are key-value pairs you must include in your API request for it to work. The Minehut API uses these to understand what kind of data you accept, verify your identity (if needed), or follow its formatting rules. For public endpoints like fetching server info, common required headers might include Accept: application/json (telling the server you want a JSON response). Always check the Minehut API docs for the exact headers listed under "Required" for each endpoint.

Code Example: Fetch Player Count

Here's how to send a GET request, parse the response, and show the player count on your website:

// Replace with your actual server ID
const serverId = "your-server-id-123";
const apiUrl = `https://api.minehut.com/server/${serverId}`;

// Required headers from the Minehut API docs
const requestHeaders = {
  "Accept": "application/json"
};

// Send the GET request
fetch(apiUrl, {
  method: "GET",
  headers: requestHeaders
})
.then(response => {
  // Check if the request was successful
  if (!response.ok) {
    throw new Error(`Request failed: ${response.status}`);
  }
  // Convert the response to JSON
  return response.json();
})
.then(serverData => {
  // Extract the player count from the response
  const playerCount = serverData.playercount;
  // Update your website to display the number
  const displayElement = document.getElementById("player-count");
  if (displayElement) {
    displayElement.textContent = `Current Players: ${playerCount}`;
  }
})
.catch(error => {
  // Handle any errors (like invalid server ID or network issues)
  console.error("Failed to fetch server info:", error);
});

How to Use This:

  1. Replace your-server-id-123 with your actual Minehut server ID.
  2. Add an element to your HTML with the ID player-count (e.g., <p id="player-count">Loading...</p>).
  3. Put this script in your HTML (either in a <script> tag or an external JS file).

Question 2: Send Commands via POST Request

Sending commands requires a POST request with specific authentication headers. Let's break down how to set this up.

Key Headers Explained

The Minehut API requires three headers for this endpoint:

  • Content-Type: application/json: Tells the server you're sending JSON data in the request body.
  • Authorization: Your personal API token (you'll get this from your Minehut account settings or by logging in via the API).
  • x-session-id: A session ID tied to your authenticated Minehut session (also obtained during login).

Code Example: Send a Command

Here's how to send a command like say Hello! to your server:

const serverId = "your-server-id-123";
const apiUrl = `https://api.minehut.com/server/${serverId}/send_command`;

// Replace these with your actual auth values from Minehut
const authToken = "your-auth-token-here";
const sessionId = "your-session-id-here";

const requestHeaders = {
  "Content-Type": "application/json",
  "Authorization": authToken,
  "x-session-id": sessionId
};

// The command you want to run
const commandToSend = "say Hello from the Minehut API!";

// Send the POST request
fetch(apiUrl, {
  method: "POST",
  headers: requestHeaders,
  // Convert the command to a JSON string for the request body
  body: JSON.stringify({ command: commandToSend })
})
.then(response => {
  if (!response.ok) {
    throw new Error(`Command failed: ${response.status}`);
  }
  return response.json();
})
.then(responseData => {
  console.log("Command sent successfully!", responseData);
  // Add success feedback here (e.g., a message on your site)
})
.catch(error => {
  console.error("Failed to send command:", error);
});

Important Notes:

  • You'll need to authenticate with the Minehut API first to get your Authorization token and x-session-id. Check the Minehut docs for how to log in via the API (usually a POST request to a login endpoint with your credentials).
  • Make sure to keep your auth token and session ID secure—never hardcode them in public-facing code! Use environment variables if you're using a framework like React, or keep them server-side if possible.

内容的提问来源于stack exchange,提问作者jontus technology

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:14:52