10

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

Gunaseelan
  • 14,415
  • 11
  • 80
  • 128

3 Answers3

4

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/

Kenyon Tu
  • 301
  • 2
  • 7
  • Can you post some example which should work in fiddler. Because I want to confirm that the API works perfectly, before start the code. – Gunaseelan Feb 06 '16 at 04:20
1

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
imriqwe
  • 1,455
  • 11
  • 15
  • I am getting following error while try this `POST methods expects all parameters to be submitted as a form, not on the query string` – Gunaseelan Feb 05 '16 at 04:56
  • It says not on the query string, Again you are passing all parameters on query string. – Gunaseelan Feb 05 '16 at 09:12
0

Your Json Data Should be send like this,it is not allowing next line

{"key":"mykey","access_token":"mytoken","site":"stackoverflow.com","preview":"false","filter":"default"}
Hithesh
  • 441
  • 4
  • 9