简体   繁体   English

使用请求模块将数据发布到带有 node.js 的 mojang api

[英]POST data to the mojang api with node.js using request module

I am using node.js for a discord bot and I want to use the Request module to post a name to the mojang api and get a uuid back.我正在将 node.js 用于 discord 机器人,并且我想使用请求模块向 mojang api 发布名称并获取 uuid。 here is my code:这是我的代码:

var uuid = request.post({
        url:     'https://api.mojang.com/profiles/minecraft',
        name:    `${username}`
    }, function(error, response, body){
        console.log(body);
        return response.id;
    });

response from body not response.身体的反应不是反应。

var uuid = request.post({
        url:     'https://api.mojang.com/profiles/minecraft',
         name:    `${username}`
    }, function(error, response, body){
        return body.id;
    });

Like @willymarj said, the data is returned in the body, not the response object.就像@willymarj 所说,数据在正文中返回,而不是响应 object。 You will also want to parse the JSON response into a javascript object.您还需要将 JSON 响应解析为 javascript object。

var uuid = request.post({
        url:     'https://api.mojang.com/profiles/minecraft',
         name:    `${username}`
    }, function(error, response, body){
        return JSON.parse(body.id);
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM