David
David

Reputation: 1293

Serialized JSON request results in "Bad Request" error

I'm using Newtonsoft to try and serialize some JSON to do a HttpWebRequest POST

I keep getting a response saying 'Bad Request'

I'm assuming my JSON is badly formed. Below is my code:

Account account = new Account();
account.Name = "TESTACCOUNT";

var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://app01.nutshell.com/api/v1/json");
httpWebRequest.ContentType = "text/json";
httpWebRequest.Method = "POST";
httpWebRequest.Credentials = new NetworkCredential("username", "password");

var serializer = new JsonSerializer();

using (var tw = new Newtonsoft.Json.JsonTextWriter(streamWriter))
{

    serializer.Serialize(tw,
                 new
                 {
                   method = "newAccount",
                   @params = account                                      
                  });
}

var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
    var result = streamReader.ReadToEnd();
}

Upvotes: 1

Views: 1184

Answers (1)

lathonez
lathonez

Reputation: 2443

If it is just the JSON:

{
    "method": "newAccount",
    "@params": "account"
}

http://pro.jsonlint.com/

Upvotes: 1

Related Questions