1

I am trying to create a question in StackOverFlow using write-access API(https://api.stackexchange.com/2.2/questions/add). I have provided the parameters as required but a "400(Bad Request)" error is being returned on firing a POST request. While I have tried to create the question through the API Console,with same "key" and "access_token" it works.I cannot understand what I am missing while creating the question through code.Following are the parameters that I have passed fro code:

API: https://api.stackexchange.com/2.2/questions/add
Parameters:

                access_token: "TOKEN))" 
                body: "This is a test question body" 
                client_id: "3980" 
                key: "KEY((" 
                site: "stackoverflow" 
                tags: ".NET" 
                title: "This is a test title"
                preview: "true"

Could anybody please help me with a solution?

5
  • Could anybody please help me on this? I am stuck up in posting questions for hours. Tried every alternatives.Need a solution ASAP. Dec 8, 2014 at 4:10
  • Edit your question to provide an MCVE. You did form-encode the parameters, right? Dec 8, 2014 at 5:46
  • Thanks for the response.I think encoding is done. The url is as following: api.stackexchange.com/2.2/questions/… Are you talking about any special kind of encoding? Dec 8, 2014 at 8:27
  • I got a better information on the 400 error. The error message from the server is: "POST methods expects all parameters to be submitted as a form, not on the query string". I am firing the POST request using C# code. Could anybody provide a C# or other langauge's code snippet to call a stackexchange API using POST? Dec 8, 2014 at 11:48
  • I have added following content header from my C# code: client.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); Please help me solving the issue. Dec 8, 2014 at 14:22

1 Answer 1

1

I am just 2 years late to answer your question :-( but this should help for anyone else looking for a C# solution.

    private static async Task<Type> PostToStackOverflowAsync<Type>(string url, Dictionary<string, string> values) where Type : new()
    {
        string jsonText;
        try
        {
            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.IfModifiedSince = DateTime.UtcNow;
                var content = new FormUrlEncodedContent(values);
                var response = await client.PostAsync(url, content);
                using (var stream = await response.Content.ReadAsStreamAsync())
                {
                    using (var zipstream = new GZipStream(stream, CompressionMode.Decompress))
                    {
                        using (var reader = new StreamReader(zipstream))
                        {
                            jsonText = reader.ReadToEnd();
                        }
                    }
                }
            }
            return JsonConvert.DeserializeObject<Type>(jsonText);
        }
        catch (Exception )
        {
            //  throw ex;
            return new Type();
        }
    }   



   var dict = new Dictionary<string, string>()
                {                    
                    {"preview", "false"},                    
                    {"access_token", accessToken},
                      {"site", "stackoverflow"},
                    {"key", key_}
                };   

   string url = "https://api.stackexchange.com/{0}/answers/{1}/upvote";
   PostToStackOverflowAsync<T>(string.Format(url, version_, id), dict);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .