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.
I think what you have is telling the ASP.Net to look for a property called value
in the POSTed data.
I'd personally let ASP.Net handle the deserialization automatically.
[HttpPost]
public int Post(Incident incident)
{
Process(incident);
return 0;
}
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.