1

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.
7
  • Can you please share the text you are getting before Deserialize and what should come in response. If you can share that then probable i will help you out correctly. Commented Nov 15, 2017 at 12:02
  • "localhost:25657/api/RaiseNew" Aren't you missing an extension .php here? Commented Nov 15, 2017 at 12:02
  • 1
    ASP.NET Web API uses Json.NET. Why are you using the deprecated JavascriptSerializer? In fact, Web API will deserialize the object automatically if the content is valid and you change the input parameter's type to Incident Commented Nov 15, 2017 at 12:07
  • WebClient is deprecated too. You don't need to write the Json string to disk before sending it either. Use the current HttpClient. Commented Nov 15, 2017 at 12:13
  • As for why value is null, it's impossible to tell without the Json string and the Incident class. 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 Commented Nov 15, 2017 at 12:14

1 Answer 1

2

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;
}
Sign up to request clarification or add additional context in comments.

4 Comments

I tried this, instead it now gives a System.Net.WebException: The remote server returned an error: (500) Internal Server Error
@Lefti try debugging the server side code. Try adding a break point on the call to Process(incident) is incident populated as you'd expect?
Are you sure you're POSTing data? If that request is GET, it'd explain your 500 error.
@RichBryant Hmm, I would have expected a 404

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.