none
Not able to define Rest Service method that takes string parameter which accepts string in payload and not from url RRS feed

  • Question

  • Hi,

    I need to have rest service defined as below.

            [OperationContract]
            [XmlSerializerFormat]
            [WebInvoke(
                Method = "PUT",
                RequestFormat = WebMessageFormat.Xml,
                BodyStyle = WebMessageBodyStyle.Bare,
                UriTemplate = "/TestData")]
            string Test(string data);

    Url should not accept string parameter. I tried to call this method in fiddler as below.

    Url PUT method: http://localhost/TestService.svc/TestData

    Header: Content-Type: text/plain

    Body: Hi

    I am getting 400 response error.

    I tried with Content-Type: application/xml, Still not working.

    It works fine when Test method accepts some User defined type as parameter and while consuming that service, client passes it as serialized string in body.

    Any suggestions on this would be helpful.

    Thanks..

    Harish


    Harish


    Monday, January 18, 2016 12:16 PM

Answers

  • Hi Harish
    I create a Restful Service project and test it with console application. It works.

    We can use the PUT method to pass parameter of String Type.

    You need to Change the UriTemplate like below:

    Here is my service code:

    [OperationContract]    
            
            [WebInvoke(Method = "PUT", UriTemplate = "/Test",
              RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)]
            string Test(string value);
      public string Test(string value)
            {
                return value += "..PutString get called";
            }

    My client code:

    string ServiceUrl;
                    ServiceUrl = @"http://LocalHost:7789/MySericeDemo/";
                    WebClient WC = new WebClient();
                    WC.Headers["Content-type"] = "application/xml";
    
                    MemoryStream MS = new MemoryStream();
                    DataContractSerializer JSrz = new DataContractSerializer(typeof(string));
                    string NAME = "ABC";
                    JSrz.WriteObject(MS, NAME);
                    byte[] res1 = WC.UploadData(ServiceUrl + "Test", "PUT", MS.ToArray());
    
                    MS = new MemoryStream(res1);
                    JSrz = new DataContractSerializer(typeof(string));
                    string result = (string)JSrz.ReadObject(MS);
    
                    Console.WriteLine(result);Result:

    Result:


    If I miss understanding your question, please let me know.

    Best Regards,

    Wanjun Dong (Pactera Technologies)


    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.



    Wednesday, January 20, 2016 3:34 AM
    Moderator

All replies

  • Hi Harish
    I create a Restful Service project and test it with console application. It works.

    We can use the PUT method to pass parameter of String Type.

    You need to Change the UriTemplate like below:

    Here is my service code:

    [OperationContract]    
            
            [WebInvoke(Method = "PUT", UriTemplate = "/Test",
              RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)]
            string Test(string value);
      public string Test(string value)
            {
                return value += "..PutString get called";
            }

    My client code:

    string ServiceUrl;
                    ServiceUrl = @"http://LocalHost:7789/MySericeDemo/";
                    WebClient WC = new WebClient();
                    WC.Headers["Content-type"] = "application/xml";
    
                    MemoryStream MS = new MemoryStream();
                    DataContractSerializer JSrz = new DataContractSerializer(typeof(string));
                    string NAME = "ABC";
                    JSrz.WriteObject(MS, NAME);
                    byte[] res1 = WC.UploadData(ServiceUrl + "Test", "PUT", MS.ToArray());
    
                    MS = new MemoryStream(res1);
                    JSrz = new DataContractSerializer(typeof(string));
                    string result = (string)JSrz.ReadObject(MS);
    
                    Console.WriteLine(result);Result:

    Result:


    If I miss understanding your question, please let me know.

    Best Regards,

    Wanjun Dong (Pactera Technologies)


    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.



    Wednesday, January 20, 2016 3:34 AM
    Moderator
  • Thanks for info

    Harish

    Thursday, February 4, 2016 11:23 AM