I'm trying to send a object via JSON to a web API but I keep running into and exception
My client side action is:
public string SubmitNewIncident(Incident input)
{
string response = String.Empty;
string serialisedJSON = String.Empty;
input.Type = 0;
serialisedJSON = JsonConvert.SerializeObject(input);
string fpath = String.Format(@"C:\dev\serialisedJSONLog_{0}.txt", DateTime.Now.ToString("yyyy-dd-M--HH-mm-ss"));
System.IO.File.WriteAllText(fpath, serialisedJSON);
using (WebClient wc = new WebClient())
{
wc.Headers[HttpRequestHeader.ContentType] = "application/json";
try
{
response = wc.UploadString(new Uri("http://localhost:25657/api/RaiseNew"), serialisedJSON);
}
catch(Exception ex)
{
string path = String.Format(@"C:\dev\ErrorLog_{0}.txt", DateTime.Now.ToString("yyyy-dd-M--HH-mm-ss"));
System.IO.File.WriteAllText(path, ex.ToString());
throw ex;
}
}
return (response);
}
The JSON string looks ok, but is very long. My server side code is:
public class RaiseNewController : ApiController
{
// GET api/raisenew
[HttpGet]
public HttpStatusCode Get()
{
return HttpStatusCode.OK;
}
//POST api/raisenew
[HttpPost]
public int Post([FromBody] Incident input)
{
input.AssignedTo = AssignNewTicket(input.AppID ?? 0);
return 0;
}
The value of the string serialisedJSON is too long to post here
When I call the action and upload the JSON string I get the following exception:
System.Net.WebException: The remote server returned an error: (500) Internal Server Error.
Incidentvalueis null, it's impossible to tell without the Json string and theIncidentclass. There are duplicates that show how to send an object to Web API, like this one that explain how to create the data class and how to map the properties