0

I have a web service as below,

  [ServiceContract]
        public interface IRestServiceImpl
        {             
            [OperationContract]
            [WebInvoke(Method = "POST",
                ResponseFormat = WebMessageFormat.Json,
                BodyStyle = WebMessageBodyStyle.Wrapped,
                UriTemplate = "json/{jsondata}")]
            void JSONData(string jsondata);       
        }


    public class RestServiceImpl : IRestServiceImpl
    {
        List<ClsTripAdvisorData> lst = new List<ClsTripAdvisorData>();
        ClsTripAdvisorData _ClsTripAdvisorData = null;

        #region IRestServiceImpl Members       
        public void JSONData(string jsondata)
        {
            string[] data = jsondata.Split('&');
        }
}

Its expecting a JSON input. while i am testing this service with poster by passing a JSON string as query string request as below,

http://localhost:1162/RestServiceImpl.svc/json/api_version=4 &hotels=[{"ta_id":97497,"partner_id":"229547","partner_url":"http://partner.com/deeplink/to/229547"},{"ta_id":97832,"partner_id":"id34234","partner_url":"http://partner.com/deeplink/to/id34234"}] &start_date=2013-07-01 &end_date=2013-07-03 &num_adults=2 &num_rooms=1 &lang=en_US &currency=USD &user_country=US &device_type=d &query_key=6167a22d1f87d2028bf60a8e5e27afa7_191_1360299600000_2_2

Here my string parameter is api_version=4 &hotels=[{"ta_id":97497,"partner_id":"229547","partner_url":"http://partner.com/deeplink/to/229547"},{"ta_id":97832,"partner_id":"id34234","partner_url":"http://partner.com/deeplink/to/id34234"}] &start_date=2013-07-01 &end_date=2013-07-03 &num_adults=2 &num_rooms=1 &lang=en_US &currency=USD &user_country=US &device_type=d &query_key=6167a22d1f87d2028bf60a8e5e27afa7_191_1360299600000_2_2

Its not hitting the service method break point while debugging.

At the same time its working for the following JSON as below,

http://localhost:1162/RestServiceImpl.svc/json/asd

The service method not taking the JSON string input,

I just want send this string as body part of my web service.But i dont know how to send and receive this json string using wcf rest service

Mansinh
  • 1,365
  • 4
  • 19
  • 47
  • 1
    Since the Method is POST it should get the JSON as content of the call rather that as part of the URL (which would be the right thing to do if you were using GET) – Daniel Oct 11 '13 at 09:57
  • @simpleBob:sorry to say but i did't get what you are saying....if possible than give me some example – Mansinh Oct 11 '13 at 10:02
  • The method to call a WS with parameters as part of the URL, for example `www.myWsURL.com?var1="wohoo"&var2="meh"` is called GET. When you set `Method = "POST"`you are supposed to attach the data to the body of the call (see [this example](http://stackoverflow.com/a/11451086/386738)) – Daniel Oct 11 '13 at 10:57
  • @simpleBob:okey...thanks but i am using poster for sending data and when i attach my data to body it will give me error like "Endpoint not found. Please see the help page" – Mansinh Oct 11 '13 at 11:24

1 Answers1

1

Add the following decoration to your method

<OperationContract()> _
    <WebGet(UriTemplate:="YourCoolFunction?inpt={inpt}", BodyStyle:=WebMessageBodyStyle.Wrapped,
            RequestFormat:=WebMessageFormat.Json, ResponseFormat:=WebMessageFormat.Xml)> _
    Public Function YourCoolFunction(inpt As String) As String

In the .svc file, add Factory="System.ServiceModel.Activation.WebServiceHostFactory"

In web.config add

<defaultDocument>
  <files>
    <add value="YourFile.svc"/>
  </files>
</defaultDocument>
lcryder
  • 486
  • 2
  • 8
  • but this is get method and i want post method ......actully i have json string and i want post this string in to my service using rest web service...i search in google in found i have to send this data in to body part but how i dont know – Mansinh Oct 16 '13 at 12:51
  • Like this(?): http://go4answers.webhost4life.com/Example/fetching-request-body-rest-json-output-75574.aspx – lcryder Oct 16 '13 at 16:09
  • yes some what like this...and i tried this code but when i send data to my service it ll give error like The remote server returned an error: (404) Not Found. – Mansinh Oct 17 '13 at 06:07
  • :yes this method working for given json but its not working on my string ......my string is api_version=4 &hotels=[{"ta_id":97497,"partner_id":"229547","partner_url":"http://partner.com/deeplink/to/229547"},{"ta_id":97832,"partner_id":"id34234","partner_url":"http://partner.com/deeplink/to/id34234"}] &start_date=2013-07-01 &end_date=2013-07-03 &num_adults=2 &num_rooms=1 &lang=en_US &currency=USD &user_country=US &device_type=d &query_key=6167a22d1f87d2028bf60a8e5e27afa7_191_1360299600000_2_2 – Mansinh Oct 17 '13 at 07:25