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

4
  • 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) Commented Oct 11, 2013 at 9:57
  • @simpleBob:sorry to say but i did't get what you are saying....if possible than give me some example Commented Oct 11, 2013 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) Commented Oct 11, 2013 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" Commented Oct 11, 2013 at 11:24

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

4 Comments

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
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.
: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":"partner.com/deeplink/to/229547"},{"ta_id":97832,"partner_id":"id34234","partner_url":"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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.