前言
本文章使用Nodejs环境做演示。请求用户数据。代码中的APIKEY要在osu官网申请。
const API_URL = "osu.ppy.sh";
const init = {
k: "YOUR_API_KEY",
type: "string",
u: "kyzzz5658",
};
const https = require("https");
// 将参数序列化为查询字符串
const query = new URLSearchParams(init);
const options = {
hostname: API_URL,
method: "GET",
path: `/api/get_user?${query}` // 附加参数
};
const req = https.request(options, (res) => {
let data = '';
res.on('data', (chunk) => data += chunk);
res.on('end', () => {
try {
console.log(JSON.parse(data[0].username));
} catch (e) {
console.error('JSON 解析失败:', e);
}
});
});
// 错误处理
req.on('error', (err) => {
console.error('请求失败:', err.code);
});
输出:
……
Continue reading