none
WCF REST - WebInvoke POST method - Receiving JSON Parameter as null in service side RRS feed

  • Question

  • Hi,

    I am not receiving complete object when sending request to WCF REST service through .NET client application and receiving null as inner object on the service side. But when I am sending same request to same service through fiddler, I am receiving the complete object on the service side.

    I have following configuration  :

    WCF Rest services are self hosted Windows Service, binding mode - webHttpBinding .NET Framework - 4.6 64bit.

    My service contract, service implementation and data contract code as below -

    Service contract :- 

            [OperationContract]
            [WebInvoke(Method = "POST",
                        UriTemplate = "/LoginRest",
                        RequestFormat = WebMessageFormat.Json,
                        ResponseFormat = WebMessageFormat.Json,
                        BodyStyle = WebMessageBodyStyle.Bare)]
            SecurityTokenRest LoginRest(LoginInfo login);

    Data contract :- 

    [DataContract]
    public class LoginInfo
    {
        [DataMember]
        public Headers headers { get; set; }
    }
    
    [DataContract]
    public class Headers
    {
         [DataMember]
         public string loginname { get; set; }
    
         [DataMember]
         public string password { get; set; }
    }

    Service Implementation :-

    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class BLTSecurityManager : MarshalByRefObject, IBLTSecurityManager
    {
       public SecurityTokenRest LoginRest(LoginInfo login)
       {
           SecurityTokenRest token = new SecurityTokenRest();
    
           if (login != null)
           {
              //token generation logic here
           }
    
           return token;
        }
    }

    When I am hitting service URL through fiddler I am receiving the complete object. Below request through fiddler -

    POST http://localhost:9990/BLTSecurityManager/Rest/LoginRest HTTP/1.1
    User-Agent: Fiddler
    Content-Type: application/json; charset=UTF-8
    Host: localhost:9990
    Content-Length: 58

    {"headers":{"loginname":"truser01","password":"password"}}

    But when I am hitting the same URL through .NET client application, I am receiving null as inner headers object. Below .NET client code and once it hits the service URL in service side I am receiving nested headers object as null -

    .NET Client code :-

    public void GetData(LoginInfo login)
    {
      using (HttpClient client = new HttpClient())
      {
        //POST Request
        string requestUrl = "http://localhost:9990/BLTSecurityManager/Rest/LoginRest";
        string jsonString = JsonConvert.SerializeObject(login);
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    
        var request = new StringContent(jsonString, Encoding.UTF8, "application/json");
        var response = client.PostAsJsonAsync(requestUrl, request).Result;
        var result = response.Content.ReadAsStringAsync();
    
        //Remaining code here
    }
    LoginInfo login = new LoginInfo();
    login.headers = new Headers();
    login.headers.loginname = txtUserName.Text;
    login.headers.password = txtPWD.Text;
    
    GetData(login);

    I am totally clueless

    Please help...Any ideas why this happens are very welcome.

    Thanks in advance!


    • Edited by Suparpal Tuesday, January 17, 2017 12:19 PM Added missing code
    Tuesday, January 17, 2017 12:14 PM

Answers

  • Hi Suparpal,

    You need to use client.PostAsync with json string or client.PostAsJsonAsync with class object.

    Here is two options for you.
    First option.

                using (HttpClient client = new HttpClient())
                {
                    //POST Request
                    string requestUrl = "http://localhost:39668/RestService.svc/LoginRest";
                    LoginInfo login = new LoginInfo();
                    Headers headers = new Headers() { loginname = "test", password = "tt" };
                    login.headers = headers;
                    string jsonString = JsonConvert.SerializeObject(login);
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    var request = new StringContent(jsonString, Encoding.UTF8, "application/json");
                    var response = client.PostAsync(requestUrl, request).Result;
                    var result = response.Content.ReadAsStringAsync();
                }

    Second option.

                using (HttpClient client = new HttpClient())
                {
                    //POST Request
                    string requestUrl = "http://localhost:39668/RestService.svc/LoginRest";
                    LoginInfo login = new LoginInfo();
                    Headers headers = new Headers() { loginname = "test", password = "tt" };
                    login.headers = headers;
                    string jsonString = JsonConvert.SerializeObject(login);
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    var request = new StringContent(jsonString, Encoding.UTF8, "application/json");
                    var responseObject = client.PostAsJsonAsync(requestUrl, login).Result;
                    var resultObject = responseObject.Content.ReadAsStringAsync();
                }

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Suparpal Wednesday, January 18, 2017 6:28 AM
    Wednesday, January 18, 2017 4:47 AM

All replies

  • Hi Suparpal,

    You need to use client.PostAsync with json string or client.PostAsJsonAsync with class object.

    Here is two options for you.
    First option.

                using (HttpClient client = new HttpClient())
                {
                    //POST Request
                    string requestUrl = "http://localhost:39668/RestService.svc/LoginRest";
                    LoginInfo login = new LoginInfo();
                    Headers headers = new Headers() { loginname = "test", password = "tt" };
                    login.headers = headers;
                    string jsonString = JsonConvert.SerializeObject(login);
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    var request = new StringContent(jsonString, Encoding.UTF8, "application/json");
                    var response = client.PostAsync(requestUrl, request).Result;
                    var result = response.Content.ReadAsStringAsync();
                }

    Second option.

                using (HttpClient client = new HttpClient())
                {
                    //POST Request
                    string requestUrl = "http://localhost:39668/RestService.svc/LoginRest";
                    LoginInfo login = new LoginInfo();
                    Headers headers = new Headers() { loginname = "test", password = "tt" };
                    login.headers = headers;
                    string jsonString = JsonConvert.SerializeObject(login);
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    var request = new StringContent(jsonString, Encoding.UTF8, "application/json");
                    var responseObject = client.PostAsJsonAsync(requestUrl, login).Result;
                    var resultObject = responseObject.Content.ReadAsStringAsync();
                }

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Suparpal Wednesday, January 18, 2017 6:28 AM
    Wednesday, January 18, 2017 4:47 AM
  • Hi Edward,

    Fantastic. Your both the solution work like a champ.

    Thanks a ton..!!

    Regards,

    Suparna

    Wednesday, January 18, 2017 6:30 AM