none
Invalid request in Resp Api with Post RRS feed

  • Question

  •         [OperationContract]
            [WebInvoke(Method = "POST",
            ResponseFormat = WebMessageFormat.Json,
            RequestFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "/DemoInsert")]
            int DemoInsert(Demo demo);
    
    //Implementation
    
     public int DemoInsert(Demo demo)
            {
                DriverDetails driver = new DriverDetails();
                return driver.DemoInsert(demo);
            }
    
    [DataContract]
        public class Demo
        {
            [DataMember]
            public string name { get; set; }
            [DataMember]
            public DateTime dob { get; set; }
            [DataMember]
            public int cid { get; set; }
            [DataMember]
            public string address { get; set; }
    
        }
    
    /* I am trying to consume the service in the console application and getting 405 error, when i am trying to access the service in the console.
    
    The basic idea if this service works fine i need to consume this service in andriod studio to do an isert.*/
    
    //Calling in the Console
    
     Demo d = new  Demo();
     d.address = "Address1";
     d.cid = 11;
     d.dob = DateTime.Today;
     d.name = "gopi";
    
    WebClient client = new WebClient();
    client.Headers["Content-type"] = "application/json";
     MemoryStream stream1 = new MemoryStream();
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Demo));
                serializer.WriteObject(stream1, d);
                byte[] data = client.UploadData("http://172.16.8.99:35802/IRestService.svc/DemoInsert", "POST", stream1.ToArray());
    
                stream1 = new MemoryStream(data);
                serializer = new DataContractJsonSerializer(typeof(Demo));
                string result = (string)serializer.ReadObject(stream1);
    

    Tuesday, April 11, 2017 7:39 PM

All replies

  • Hi Its me Gopi,

    Thank you for posting here.

    According to your question is more related to WCF, I will move it to  Windows Communication Foundation, Serialization, and Networking forum for suitable support.

    The Visual C# discuss and ask the C# programming language, IDE, libraries, samples and tools.

    If you have some grammar or code errors, please feel free to contact us. We will try our best to give you a solution.

    Thanks for your understanding and cooperation. 

    Best Regards,

    Wendy


    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.

    Thursday, April 13, 2017 2:09 AM
  • For sending request to Rest Service by WebClient, I suggest you try below code:

                Demo d = new Demo();
                d.address = "A1";
                d.cid = 11;
                d.dob = DateTime.Today;
                d.name = "gopi";
                WebClient client = new WebClient();
                client.Headers["Content-type"] = "application/json";
                string url = @"http://localhost:61378/Service1.svc/demoinsert";                        
                string data = JsonConvert.SerializeObject(d);
                var response = client.UploadString(url,"POST", data);


    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.

    Thursday, April 13, 2017 8:59 AM
  • Giving me error   The remote server returned an error: (400) Bad Request. I got stuck with this. Some could please help me on this.
    Thursday, April 13, 2017 1:38 PM
  • What is your current code?

    To check whether it is specific to DemoInsert method, I suggest you add a simple method which returns a string by Get, and access this method in IE to see whether it will work.

    To check whether it is related with your code, I suggest you try PostMan to send request with Demo Json object.

    In addition, I suggest you create a new Rest Service with this method which will accept Demo parameter and return 1 to check whether it will work.


    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.

    Friday, April 14, 2017 2:26 AM