14

I have a very simple Web API 4 controller over some legacy database code. The entity like like this:

public class Employee
{
    public string EmploymentStatus { get; set; }
    public string CompanyCode { get; set; }
    public string Division { get; set; }
    public string OrgLevel1Code { get; set; }
    public string OrgLevel2Code { get; set; }
    public string OrgLevel3 { get; set; }
    public string StoreName { get; set; }
    public string EmployeeNumber { get; set; }
    public string EmployeeFirstName { get; set; }
    public string EmployeeMiddleInitial { get; set; }
    public string EmployeeLastName { get; set; }
    public string EmailAddress { get; set; }
    public string JobCode { get; set; }
    public string DateInJob { get; set; }
    public string OriginalHire { get; set; }
}

The method looks like this:

    public HttpResponseMessage PostEmployee(Employee item)
    {
        DataHelpers.AddUser(item.CompanyCode, item.Division, item.OrgLevel1Code, item.OrgLevel2Code, item.OrgLevel3, item.EmployeeFirstName, item.EmployeeMiddleInitial, item.EmployeeLastName, item.EmailAddress, item.JobCode, item.OriginalHire);
        var response = Request.CreateResponse<Employee>(HttpStatusCode.Created, item);
        string uri = Url.Link("DefaultApi", new { id = item.EmployeeNumber });
        response.Headers.Location = new Uri(uri);
        return response;
    }

When I POST through Fiddler like this:

POST /api/identity HTTP/1.1
User-Agent: Fiddler
Host: localhost:1421
Content-Length: 382
contentType: "application/json; charset=utf-8"
dataType: 'json'

{
"employmentStatus":"zzz",
"companyCode":"Titlemax",
"division":"bbb",
"orgLevel1Code":"ccc",
"orgLevel2Code":"ddd",
"orgLevel3":"eee",
"storeName":"fff",
"employeeNumber":"12343",
"employeeFirstName":"Bill",
"employeeMiddleInitial":"A",
"employeeLastName":"sempf",
"emailAddress":"[email protected]",
"jobCode":"GM",
"dateInJob":"8/7/2005",
"originalHire":"8/7/2005"
}

I get an exception from .NET and the item parameter is null.

{"Message":"An error has occurred.","ExceptionMessage":"Object reference not set to an instance of an object.","ExceptionType":"System.NullReferenceException"}

What am I missing? I'm new to the Web API. Thanks in advance.

2
  • You could debug into your method PostEmployee to see which code line throw this exception?
    – cuongle
    Feb 21, 2013 at 3:29
  • 1
    The item parameter is null. So the exception occurs when I reference it.
    – Bill Sempf
    Feb 21, 2013 at 3:33

2 Answers 2

29

I think it is the request format in Fiddler. Try removing the quotes from the Content-Type header

From the Composer tab:

POST http://localhost:1421/api/identity HTTP/1.1

Request Headers:

User-Agent: Fiddler
Host: localhost:1421
Content-Type: application/json; charset=utf-8

Request Body:

{
   "employmentStatus":"zzz",
   "companyCode":"Titlemax",
   "division":"bbb",
   "orgLevel1Code":"ccc",
   "orgLevel2Code":"ddd",
   "orgLevel3":"eee",
   "storeName":"fff",
   "employeeNumber":"12343",
   "employeeFirstName":"Bill",
   "employeeMiddleInitial":"A",
   "employeeLastName":"sempf",
   "emailAddress":"[email protected]",
   "jobCode":"GM",
   "dateInJob":"8/7/2005",
   "originalHire":"8/7/2005"
}

Response:

HTTP/1.1 201 Created
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Location: http://localhost:1421/api/identity/12343
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?YzpcdXNlcnNcY2FsdmlfMDAwXGRvY3VtZW50c1x2aXN1YWwgc3R1ZGlvIDIwMTJcUHJvamVjdHNcTXZjQXBwbGljYXRpb24yXE12Y0FwcGxpY2F0aW9uMlxhcGlcaWRlbnRpdHk=?=
X-Powered-By: ASP.NET
Date: Thu, 21 Feb 2013 03:53:04 GMT
Content-Length: 351

{"EmploymentStatus":"zzz","CompanyCode":"Titlemax","Division":"bbb","OrgLevel1Code":"ccc","OrgLevel2Code":"ddd","OrgLevel3":"eee","StoreName":"fff","EmployeeNumber":"12343","EmployeeFirstName":"Bill","EmployeeMiddleInitial":"A","EmployeeLastName":"sempf","EmailAddress":"[email protected]","JobCode":"GM","DateInJob":"8/7/2005","OriginalHire":"8/7/2005"}
1
  • 5
    Listen here kids - this is a cautionary tale of how as you get older you can't work for 16 hour days anymore. Thanks, Calvin - that's exactly it. Used JavaScript code in my header. You get the green check because you did a PoC.
    – Bill Sempf
    Feb 21, 2013 at 13:05
3

Your fiddler request header should be like this:

Content-Type: application/json

1
  • And just to be completely clear - the header appears in the upper part of the Fiddler Compose dialog, right under "User-Agent" and "Content-Length" headers that Fiddler automatically adds. The body in the lower part of the Compose dialog should start with the "{" of the JSON body itself. This is probably obvious to more experienced Fiddler users but it took me a couple of times to get it right.
    – Mike Kelly
    Dec 12, 2014 at 19:09

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.