Syed Ali
Syed Ali

Reputation: 1897

Model not getting values from POST request

I am trying to POST something to my Web API but not matter what I do the model values are always set to null.

I have tried the following request (trace copied from fiddler):

POST http://localhost:53176/api/Profile HTTP/1.1
Host: localhost:53176
Connection: keep-alive
Content-Length: 40
Accept: application/xml, text/xml, */*; q=0.01
Origin: http://localhost:53176
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Referer: http://localhost:53176/SignUp
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Cookie: ASP.NET_SessionId=do5ddqwghl3v0bhlgr3pyicb

{"InputEmail":"aa","UserName":"asd"}

Update and also with the following:

POST http://localhost:53176/api/Profile HTTP/1.1
Host: localhost:53176
Connection: keep-alive
Content-Length: 44
Accept: application/json, text/javascript, */*; q=0.01
Origin: http://localhost:53176
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36
Content-Type: application/json
Referer: http://localhost:53176/SignUp
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Cookie: ASP.NET_SessionId=do5ddqwghl3v0bhlgr3pyicb

{"InputEmail":"asdas","UserName":"dasd"}

My api looks like:

    //here I have also tried [FromBody]
    public MyDto Post(MyDto input)
    {
       return input;
    }

MyDto has two properties and is:

public class MyDto 
{
    string InputEmail { get; set; }
    string UserName { get; set; }
}

Would someone be able to tell me what is the problem in this code or if there is anything that I am missing?

I would also like to know how (or where) can I put the debugger somewhere inside webapi code to see what was the actual request before the webapi tries to deserialize it.

Upvotes: 0

Views: 169

Answers (1)

Kiran
Kiran

Reputation: 57969

Your Content-Type header is application/x-www-form-urlencoded, but your body has json data. Modify your content-type header to be either application/json or text/json

EDIT: Make your properties Public

Upvotes: 1

Related Questions