Web API Test Error - HTTP/1.1 500 Internal Server Error RRS feed

  • Question

  • I am trying to test a POST method of my Web API but geting following error:

    Error in Fiddler: HTTP/1.1 500 Internal Server Error

    Error in ASP.NET app: System.Net.WebException: The remote server returned an error: (500) Internal Server Error.

    Line 45: 		HttpWebResponse httpWebResponse = webRequest.GetResponse() as HttpWebResponse;

    Here is my code within Web API:

    public class SuppliersController : ApiController


    public HttpResponseMessage PostSuppliers(SuppliersData item)
    var result = DAL.Suppliers.Add(item);
    var response = Request.CreateResponse<SuppliersData>(HttpStatusCode.Created, item);

    string uri = Url.Link("DefaultApi", new { id = item.SupplierID });
    response.Headers.Location = new Uri(uri);
    return response;


    I have tried to test this through Fiddler as well as custom method in ASP.NET web app.

    Here is my custom code within ASP.NET app.

    public void TestWebAPI()


    const string uri = "http://localhost:29604/api/Suppliers";
    var sup = new SuppliersData(0, "MyCompany", "CB", "MD", "E 53rd St", "New York", "US", "10001", "USA", "123456789", "123456789", "");

    var webRequest = (HttpWebRequest) WebRequest.Create(uri);
    webRequest.Method = "POST";
    webRequest.ContentType = "application/json";
    webRequest.Credentials = CredentialCache.DefaultCredentials;
    var deptSerialized = JsonConvert.SerializeObject(sup); // <-- This is JSON.NET;
    webRequest.ContentLength = deptSerialized.Length;
    using (StreamWriter sw = new StreamWriter(webRequest.GetRequestStream()))
    HttpWebResponse httpWebResponse = webRequest.GetResponse() as HttpWebResponse;
    using (StreamReader sr = new StreamReader(httpWebResponse.GetResponseStream()))
    string status = String.Format("StatusCode == {0}", httpWebResponse.StatusCode);
    string output = sr.ReadToEnd();


    However, I am getting the error at below line:

    HttpWebResponse httpWebResponse = webRequest.GetResponse() as HttpWebResponse;

    I have been scratching my head since 4 to 5 hours now reading many articles, but no luck so far... Please help!

    Monday, December 9, 2013 6:37 PM