Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Consuming stack exchange API

I have tried to upvote a stackoverflow question with the stack exchange api and failed. I have tried a lot but I didn't get it to work.

URL :

http://api.stackexchange.com/2.2/questions/35007869/upvote

Docs

https://api.stackexchange.com/docs/upvote-question

Json Data :

{
  "key" : "my key",
  "access_token" : "my token",
  "site" : "stackoverflow.com",
  "preview" : "false",
  "filter": "default"
}

I tried through fiddler with following parameters.

User-Agent: Fiddler
Host: api.stackexchange.com
Content-Length: 159
Content-Type: application/json; charset=utf-8

And POST method. But I am failed with following error message.

error_id=400
error_message=site is required
error_name=bad_parameter

But I have provided the site in my JSON object. So Any help will be highly appreciable.

Update

While try this in fiddler I got following message.

enter image description here

like image 662
Gunaseelan Avatar asked Jan 26 '16 20:01

Gunaseelan


2 Answers

You need to send them as form data, with Javascript it would be like this:

var request = new XMLHttpRequest();
request.open('POST', 'http://api.stackexchange.com/2.2/questions/35007869/upvote', true);

var formData = new FormData();
formData.append('key', 'my key');
formData.append('access_token', 'my token');
formData.append('site', 'stackoverflow.com');
formData.append('preview', 'false');
formData.append('filter', 'default');

request.send(formData);

Here's a guide to do it with Android: http://www.onlymobilepro.com/2013/03/16/submitting-android-form-data-via-post-method/

like image 64
Kenyon Tu Avatar answered Sep 29 '22 06:09

Kenyon Tu


You have to send the parameters as URL arguments, not as a raw JSON on the request body. In order to perform an upvote, send the following POST request:

http://api.stackexchange.com/2.2/questions/35007869/upvote?site=stackoverflow.com&key=YOUR_KEY&access_token=YOUR_TOKEN&preview=false&filter=default
like image 29
imriqwe Avatar answered Sep 29 '22 07:09

imriqwe