Reputation: 1293
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
Reputation: 2443
If it is just the JSON:
{
"method": "newAccount",
"@params": "account"
}
Upvotes: 1