使用请求库 + nodeJS 向 API 发出 运行 请求

Issues running requests to API using request library + nodeJS

我正在尝试 运行 使用 NodeJS 和请求库向 API 发送请求。

var creds = {
    json: {
        email : "user",
        pass : "pw"
    }
}

var urlPost = "https://api.domain.rocks/auth.php"
request({
        url : urlPost,
        method : 'POST',
        json : true,
        body : creds
    },
    function(error, response, body) {
        console.log(response.body)
    }
)

我收到一条错误消息,指出 creds json 对象中的参数未正确发送 ("bad email")。我已经尝试了 url 编码、查询字符串包的所有奇怪组合 ...

我在 Python 中进行了测试,这是一种我更熟悉的语言,它可以使用 Python 2.7 和请求库在那里工作。这给了我所需的身份验证令牌。

payload = {"email" : "email", "pass" : "pw"}
r = requests.post('https://api.domain.rocks/auth.php', data=payload)
rparsed = json.loads(r.text)

提前致谢

在您的 python 示例中,您发送有效载荷 {"email" : "email", "pass" : "pw"}

但在您的 JS 代码中,您的有效载荷是:

{
    "json": {
        "email" : "user",
        "pass" : "pw"
    }
}

从您的 creds 对象中删除此 json

var creds = {
    email : "user",
    pass : "pw"
}