I am trying to experiment with the API for my project.
I am trying to upvote a comment on the SO website to test out the API. My POST request is returning with a status code of 400.
The response -
{"error_id":400,"error_message":"site is required","error_name":"bad_parameter"}
I have tried putting the site parameter in the URL, using the line request_api("/comments/{id}/upvote", "site=stackoverflow.com", "POST", body); (see below code), but it said:
{"error_id":400,"error_message":"POST methods expects all parameters to be submitted as a form, not on the query string","error_name":"bad_parameter"}
Here is my code -
let key = "{key}", base_url = "https://api.stackexchange.com";
function request_api(path, params="", method="GET", body=""){
let xhr = new XMLHttpRequest();
let url;
url = base_url + path + "?" + params;
alert(url);
xhr.open(method, url, false);
xhr.setRequestHeader("Content-Type", "application/json")
xhr.onload = function(){
if (xhr.status >= 200 && xhr.status < 300){
window.responseText = xhr.responseText;
} else {
alert("Status received is " + xhr.status);
window.responseText = xhr.responseText;
};
};
xhr.onerror = function(){
alert("Error " + xhr.status);
window.responseText = xhr.responseText;
}
if (body){
xhr.send(body);
} else {
xhr.send();
};
};
let body = JSON.stringify({
access_token: login_info.access_token,
key: key,
site: "stackoverflow"
});
alert(body);
request_api("/comments/{id}/upvote", "", "POST", body);
document.write(window.responseText);
Edit: I looked at this post. I tried the solution there (it was the most upvoted and had a bounty). It didn't work.
Edit 2: Same thing with /questions/{id}/upvote