简体   繁体   中英

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();
}

If it is just the JSON:

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

http://pro.jsonlint.com/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM