Skip to content

Commit 2bfd1fb

Browse files
Jayprakash-SEsrish
authored andcommitted
Added 20 Javascript POST sample code
1 parent 9c8b30a commit 2bfd1fb

23 files changed

+1573
-60
lines changed

javascript/block_user.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
block_user.js
3+
4+
MediaWiki API Demos
5+
Demo of `Block` module: sending POST request to block user
6+
7+
MIT license
8+
*/
9+
10+
var request = require('request').defaults({jar: true}),
11+
url = "http://dev.wiki.local.wmftest.net:8080/w/api.php";
12+
13+
// Step 1: GET Request to fetch login token
14+
function getLoginToken() {
15+
var params_0 = {
16+
action: "query",
17+
meta: "tokens",
18+
type: "login",
19+
format: "json"
20+
};
21+
22+
request.get({ url: url, qs: params_0 }, function (error, res, body) {
23+
if (error) {
24+
return;
25+
}
26+
var data = JSON.parse(body);
27+
loginRequest(data.query.tokens.logintoken);
28+
});
29+
}
30+
31+
// Step 2: POST Request to log in.
32+
// Use of main account for login is not
33+
// supported. Obtain credentials via Special:BotPasswords
34+
// (https://www.mediawiki.org/wiki/Special:BotPasswords) for lgname & lgpassword
35+
function loginRequest(login_token) {
36+
var params_1 = {
37+
action: "login",
38+
lgname: "bot_username",
39+
lgpassword: "bot_password",
40+
lgtoken: login_token,
41+
format: "json"
42+
};
43+
44+
request.post({ url: url, form: params_1 }, function (error, res, body) {
45+
if (error) {
46+
return;
47+
}
48+
getCsrfToken();
49+
});
50+
}
51+
52+
// Step 3: GET request to fetch CSRF token
53+
function getCsrfToken() {
54+
var params_2 = {
55+
action: "query",
56+
meta: "tokens",
57+
format: "json"
58+
};
59+
60+
request.get({ url: url, qs: params_2 }, function(error, res, body) {
61+
if (error) {
62+
return;
63+
}
64+
var data = JSON.parse(body);
65+
block(data.query.tokens.csrftoken);
66+
});
67+
}
68+
69+
// Step 4: POST request to block user
70+
function block(csrf_token) {
71+
var params_3 = {
72+
action: "block",
73+
user: "ABCDEF",
74+
expiry: "2020-02-25T07:27:50Z",
75+
reason: "API Test",
76+
token: csrf_token,
77+
format: "json"
78+
};
79+
80+
request.post({ url: url, form: params_3 }, function (error, res, body) {
81+
if (error) {
82+
return;
83+
}
84+
console.log(body);
85+
});
86+
}
87+
88+
// Start From Step 1
89+
getLoginToken();

javascript/change_user_options.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
change_user_options.js
3+
4+
MediaWiki API Demos
5+
Demo of `Options` module: POST request to change two options
6+
for current user
7+
8+
MIT license
9+
*/
10+
11+
var request = require('request').defaults({jar: true}),
12+
url = "https://test.wikipedia.org/w/api.php";
13+
14+
// Step 1: GET Request to fetch login token
15+
function getLoginToken() {
16+
var params_0 = {
17+
action: "query",
18+
meta: "tokens",
19+
type: "login",
20+
format: "json"
21+
};
22+
23+
request.get({ url: url, qs: params_0 }, function (error, res, body) {
24+
if (error) {
25+
return;
26+
}
27+
var data = JSON.parse(body);
28+
loginRequest(data.query.tokens.logintoken);
29+
});
30+
}
31+
32+
// Step 2: POST Request to log in.
33+
// Use of main account for login is not
34+
// supported. Obtain credentials via Special:BotPasswords
35+
// (https://www.mediawiki.org/wiki/Special:BotPasswords) for lgname & lgpassword
36+
function loginRequest(login_token) {
37+
var params_1 = {
38+
action: "login",
39+
lgname: "bot_username",
40+
lgpassword: "bot_password",
41+
lgtoken: login_token,
42+
format: "json"
43+
};
44+
45+
request.post({ url: url, form: params_1 }, function (error, res, body) {
46+
if (error) {
47+
return;
48+
}
49+
getCsrfToken();
50+
});
51+
}
52+
53+
// Step 3: GET request to fetch CSRF token
54+
function getCsrfToken() {
55+
var params_2 = {
56+
action: "query",
57+
meta: "tokens",
58+
format: "json"
59+
};
60+
61+
request.get({ url: url, qs: params_2 }, function(error, res, body) {
62+
if (error) {
63+
return;
64+
}
65+
var data = JSON.parse(body);
66+
change_options(data.query.tokens.csrftoken);
67+
});
68+
}
69+
70+
// Step 4: POST request to change the user options
71+
function change_options(csrf_token) {
72+
var params_3 = {
73+
action: "options",
74+
change: "language=en|skin=timeless",
75+
token: csrf_token,
76+
format: "json"
77+
};
78+
79+
request.post({ url: url, form: params_3 }, function (error, res, body) {
80+
if (error) {
81+
return;
82+
}
83+
console.log(body);
84+
});
85+
}
86+
87+
// Start From Step 1
88+
getLoginToken();

javascript/create_account.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
create_account.js
3+
4+
MediaWiki API Demos
5+
Demo of `createaccount` module: Create an account on a wiki without the
6+
special authentication extensions
7+
8+
MIT license
9+
*/
10+
11+
var request = require('request').defaults({jar: true}),
12+
wikiUrl = "http://dev.wiki.local.wmftest.net:8080",
13+
endPoint = wikiUrl + "/w/api.php";
14+
15+
// Step 1: GET Request to fetch createaccount token
16+
function getCreateAccountToken() {
17+
var params_0 = {
18+
action: "query",
19+
meta: "tokens",
20+
type: "createaccount",
21+
format: "json"
22+
};
23+
24+
request.get({ url: endPoint, qs: params_0 }, function (error, res, body) {
25+
if (error) {
26+
return;
27+
}
28+
var data = JSON.parse(body);
29+
createaccount(data.query.tokens.createaccounttoken);
30+
});
31+
}
32+
33+
// Step 2: POST Request with the fetched token and other data (user information,
34+
// return URL, etc.) to the API to create an account
35+
function createaccount(createaccount_token) {
36+
var params_1 = {
37+
action: "createaccount",
38+
username: "your_username",
39+
password: "your_password",
40+
retype: "retype_your_password",
41+
createreturnurl: wikiUrl,
42+
createtoken: createaccount_token,
43+
format: "json"
44+
};
45+
46+
request.post({ url: endPoint, form: params_1 }, function (error, res, body) {
47+
if (error) {
48+
return;
49+
}
50+
console.log(body);
51+
});
52+
}
53+
54+
// Start From Step 1
55+
getCreateAccountToken();

javascript/delete.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
delete.js
3+
4+
MediaWiki API Demos
5+
Demo of `Delete` module: post request to delete a page
6+
7+
MIT license
8+
*/
9+
10+
var request = require('request').defaults({jar: true}),
11+
url = "http://dev.wiki.local.wmftest.net:8080/w/api.php";
12+
13+
// Step 1: GET Request to fetch login token
14+
function getLoginToken() {
15+
var params_0 = {
16+
action: "query",
17+
meta: "tokens",
18+
type: "login",
19+
format: "json"
20+
};
21+
22+
request.get({ url: url, qs: params_0 }, function (error, res, body) {
23+
if (error) {
24+
return;
25+
}
26+
var data = JSON.parse(body);
27+
loginRequest(data.query.tokens.logintoken);
28+
});
29+
}
30+
31+
// Step 2: POST Request to log in.
32+
// Use of main account for login is not
33+
// supported. Obtain credentials via Special:BotPasswords
34+
// (https://www.mediawiki.org/wiki/Special:BotPasswords) for lgname & lgpassword
35+
function loginRequest(login_token) {
36+
var params_1 = {
37+
action: "login",
38+
lgname: "bot_username",
39+
lgpassword: "bot_password",
40+
lgtoken: login_token,
41+
format: "json"
42+
};
43+
44+
request.post({ url: url, form: params_1 }, function (error, res, body) {
45+
if (error) {
46+
return;
47+
}
48+
getCsrfToken();
49+
});
50+
}
51+
52+
// Step 3: GET request to fetch CSRF token
53+
function getCsrfToken() {
54+
var params_2 = {
55+
action: "query",
56+
meta: "tokens",
57+
format: "json"
58+
};
59+
60+
request.get({ url: url, qs: params_2 }, function(error, res, body) {
61+
if (error) {
62+
return;
63+
}
64+
var data = JSON.parse(body);
65+
delete_page(data.query.tokens.csrftoken);
66+
});
67+
}
68+
69+
// Step 4: POST request to delete a page
70+
function delete_page(csrf_token) {
71+
var params_3 = {
72+
action: "delete",
73+
title: "Test",
74+
token: csrf_token,
75+
format: "json"
76+
};
77+
78+
request.post({ url: url, form: params_3 }, function (error, res, body) {
79+
if (error) {
80+
return;
81+
}
82+
console.log(body);
83+
});
84+
}
85+
86+
// Start From Step 1
87+
getLoginToken();

javascript/edit.js

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,7 @@ function getLoginToken() {
1919
format: "json"
2020
};
2121

22-
var query = url + "?";
23-
24-
Object.keys(params_0).forEach(function (key) {
25-
query += "&" + key + "=" + params_0[key];
26-
});
27-
28-
request.get(query, function (error, res, body) {
22+
request.get({ url: url, qs: params_0 }, function (error, res, body) {
2923
if (error) {
3024
return;
3125
}
@@ -47,10 +41,7 @@ function loginRequest(login_token) {
4741
format: "json"
4842
};
4943

50-
request.post({
51-
url: url,
52-
form: params_1,
53-
}, function (error, res, body) {
44+
request.post({ url: url, form: params_1 }, function (error, res, body) {
5445
if (error) {
5546
return;
5647
}
@@ -66,13 +57,7 @@ function getCsrfToken() {
6657
format: "json"
6758
};
6859

69-
var query = url + "?";
70-
71-
Object.keys(params_2).forEach(function (key) {
72-
query += "&" + key + "=" + params_2[key];
73-
});
74-
75-
request.get(query, function(error, res, body) {
60+
request.get({ url: url, qs: params_2 }, function(error, res, body) {
7661
if (error) {
7762
return;
7863
}
@@ -86,15 +71,12 @@ function editRequest(csrf_token) {
8671
var params_3 = {
8772
action: "edit",
8873
title: "Sandbox",
74+
appendtext: "test edit",
8975
token: csrf_token,
90-
format: "json",
91-
appendtext: "test edit"
76+
format: "json"
9277
};
9378

94-
request.post({
95-
url: url,
96-
form: params_3,
97-
}, function (error, res, body) {
79+
request.post({ url: url, form: params_3 }, function (error, res, body) {
9880
if (error) {
9981
return;
10082
}

0 commit comments

Comments
 (0)