none
Error executing JSON based Restful WCF Service RRS feed

  • Question

  • I am trying to write a json based restful WCF service. When i execute it using RestSharp i get a null response with the following Error in the ErrorException and the ErrorMessage. Data at the root level is invalid. Line 1, position 1.

    Status Code is BadRequest

    The data object is created as

    [DataContract]
    public class User
    {        
        [DataMember]
        public string first_name { get; set; }
    
        [DataMember]
        public string last_name { get; set; }
    
        [DataMember]
        public string email_address { get; set; }
    
        [DataMember]
        public List<UserAddress> Address;
    }
    
    [DataContract]
    public class UserAddress
    {
        [DataMember]
        public string line_1_address { get; set; }
    
        [DataMember]
        public string state { get; set; }
    
        [DataMember]
        public string PostalArea { get; set; }
    }

    The service code is

       [ServiceContract]
        public interface IUserService
        {
            [OperationContract]
            User AddUser(User user);
        }
    
    
        public class UserService : IUserService
        {
            [WebInvoke(Method = "PUT",
               RequestFormat = WebMessageFormat.Json,
               ResponseFormat = WebMessageFormat.Json,
               UriTemplate = "User/")]
            public User AddUser(User user)
            {
                return new User();
            }
        }

    The service is hosted in a Web App.

    I have a c# client based on RestSharp

        static void Main(string[] args)
        {
            User u = new User();
            u.first_name = "testFirst";
            u.last_name = "testLast";
    
            UserAddress ua = new UserAddress();
            ua.line_1_address = "line 1";
            ua.PostalArea = "08648";
            ua.state = "NJ";
    
            u.Address = new List<UserAddress>();
            u.Address.Add(ua);
    
            RestSharp.RestClient client = new RestSharp.RestClient("http://localhost/TestWCHHost/User.svc");
            var request = new RestRequest(String.Format("User/"), Method.PUT);
            request.RequestFormat = DataFormat.Json;
            request.AddObject(u);
            var response = client.Execute<User>(request);
        }

    Sunday, November 9, 2014 4:55 PM

Answers

  • Hi Siddharth Kumar,

    Please try to use the request.AddBody(u) for instead, then in order to let us can see the result very clearly, I have created the following example, please try to check it:
    The Service code:

    [WebInvoke(Method = "PUT",
                 RequestFormat = WebMessageFormat.Json,
                 ResponseFormat = WebMessageFormat.Json,
                 UriTemplate = "User/")]
            public string AddUser(User user)
            {
                return "first_name="+user.first_name;
            }

    The client code:
    static void Main(string[] args)
            {
                User u = new User();
                u.first_name = "testFirst";
                u.last_name = "testLast";
                UserAddress ua = new UserAddress();
                ua.line_1_address = "line 1";
                ua.PostalArea = "08648";
                ua.state = "NJ";
                u.Address = new List<UserAddress>();
                u.Address.Add(ua);
                RestSharp.RestClient client = new RestSharp.RestClient("http://localhost:49404/Service1.svc");
                var request = new RestRequest(String.Format("User/"), Method.PUT);
                request.RequestFormat = DataFormat.Json;
                request.AddBody(u);
                var response = client.Execute(request);
                string result = response.Content;
    
    
            }

    The result:

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, November 10, 2014 9:04 AM
    Moderator

All replies

  • Hi Siddharth Kumar,

    Please try to use the request.AddBody(u) for instead, then in order to let us can see the result very clearly, I have created the following example, please try to check it:
    The Service code:

    [WebInvoke(Method = "PUT",
                 RequestFormat = WebMessageFormat.Json,
                 ResponseFormat = WebMessageFormat.Json,
                 UriTemplate = "User/")]
            public string AddUser(User user)
            {
                return "first_name="+user.first_name;
            }

    The client code:
    static void Main(string[] args)
            {
                User u = new User();
                u.first_name = "testFirst";
                u.last_name = "testLast";
                UserAddress ua = new UserAddress();
                ua.line_1_address = "line 1";
                ua.PostalArea = "08648";
                ua.state = "NJ";
                u.Address = new List<UserAddress>();
                u.Address.Add(ua);
                RestSharp.RestClient client = new RestSharp.RestClient("http://localhost:49404/Service1.svc");
                var request = new RestRequest(String.Format("User/"), Method.PUT);
                request.RequestFormat = DataFormat.Json;
                request.AddBody(u);
                var response = client.Execute(request);
                string result = response.Content;
    
    
            }

    The result:

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, November 10, 2014 9:04 AM
    Moderator
  • Hi Amy

    Thank You so much. Worked!!!

    Monday, November 10, 2014 6:37 PM