发布对mediawiki api的请求

时间:2014-02-19 23:19:45

标签: javascript node.js mediawiki-api

我正在尝试将数据发布到mediawiki API,但它无法识别我发送的令牌。

var data = querystring.stringify({
      action: "createaccount",
      name: "sean",
      email: "xxx",
      password: "test",
      token: "66cde5ad831521fe9d0fe4df3a2db25f"
});

var options = {
    host: '54.201.91.132',
    port: 80,
    path: '/wiki/api.php',
    method: 'POST',
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Content-Length': Buffer.byteLength(data)
    }
};

var req = http.request(options, function(res) {
    res.setEncoding('utf8');
    res.on('data', function (chunk) {
        console.log("body: " + chunk);
    });
});

req.write(data);
req.end();

我尝试使用Postman chrome扩展程序发送相同的数据,这样可以正常工作。

POST /wiki/api.php HTTP/1.1
Host: 54.201.91.132
Cache-Control: no-cache
Content-Type: application/x-www-form-urlencoded
 action=createaccount&name=shamus&email=xxx&password=test&token=5dc9c943ac3255f87dc7782c24f61ac6&format=json

{
"createaccount": {
    "username": "Shamus",
    "userid": 22,
    "token": "c3744c0f19ea62f6baf89b10f7c86f7f",
    "result": "success"
}
}

任何想法我做错了什么?

1 个答案:

答案 0 :(得分:1)

除了令牌,您必须将随附的Cookie传递给它。当然,令牌不能被硬编码并动态获取。