1

I've been trying for days to find a proper way to post an object to a Web API controller method using a C# client (specifically, my Windows Forms application). Here is my code for calling the Web API with the object:

private async void insertDocument(Document doc)
{
    using (var client = new HttpClient())
    {
        var response = await client.PostAsJsonAsync(
            Constants.API_ROOT_URI + "Documents/Post", doc);
        if (response.IsSuccessStatusCode)
        {
            MessageBox.Show("Document posted successfully.");
        }
        else
        {
            MessageBox.Show("Document NOT posted successfully.");
        }
    }
}

Here my controller POST method in my Web API:

// POST api/Documents (INSERT)
public HttpResponseMessage Post([FromBody] Document doc)
{
    if (doc == null)
    {
        Request.CreateErrorResponse(HttpStatusCode.BadRequest, 
            "Could not read the Document from body");
    }
    bool success = docBL.insertDocumentEntry(doc);
    if (success)
    {
        return Request.CreateResponse(HttpStatusCode.OK);
    }
    return Request.CreateErrorResponse(HttpStatusCode.BadRequest, 
        "Could not save Document model to database");
}

And finally here is my routing template in my WebApiConfig file:

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{action}/{id}",
    defaults: new { id = RouteParameter.Optional }

I've tried many ways to do this and properly get my Document object from my C# UI client to my Web API controller method, and every way I have tried has resulted in a "StatusCode 500: Internal Server Error" from my response variable in my insertDocument method when I debug with a breakpoint. Could someone please tell me what I'm doing wrong?

10
  • Using Fiddler or other browser tools, what is the raw response from the server? Do you have possible serialization issues? Commented Aug 14, 2015 at 15:35
  • 2
    StatusCode 500: Internal Server Errormeans your web api controller is throwing an exception, have you debugged your controller action? If so what is the exception, does it get called at all? If not inspect the response and post the error message. Commented Aug 14, 2015 at 15:36
  • @BenRobinson Yes I have tested my controller with a console app in my Web API solution and I still get an internal server error. I do not know why. Commented Aug 14, 2015 at 16:01
  • Like @BenRobinson said, you need to debug the controller. Set a breakpoint. Inspect the values. See if an exception is generated in the controller or a filter on the controller or some other step of the processing. That 500 Internal Server Error is your indication that something went wrong on the server, and it's your job to find out what so that you can solve the underlying problem. Commented Aug 14, 2015 at 16:13
  • @mason How do I go about debugging the POST method in a controller other than with a console app or Postman? Postman and my web browser both throw a "The requested resource does not support http method 'GET'." exception when I try to hit Documents/Post. Is my routing off...? Commented Aug 14, 2015 at 16:37

1 Answer 1

1

I found out what I was doing wrong, I'm surprised it took me this long to see the error!

It turns out that I was not filling out all attributes of the object I was passing as a JSON to my Web API. Some objects encapsulated in the head object were not given values so the API registered the JSON as an invalid Document object.

Thanks for all the comments!!!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.