locked
How declare wcf rest - method post RRS feed

  • Question

  • User-1350042179 posted

    Hi

    with GET I use:

    [OperationContract]
    [WebInvoke(Method = "GET",
    ResponseFormat = WebMessageFormat.Json,
    BodyStyle = WebMessageBodyStyle.Wrapped,
    UriTemplate = "json/{firstn}")]
    List<Person> JSONData(string firstn);

    and with post i dont know:
    Is it correct:

    [OperationContract]
    [WebInvoke(Method = "POST",
    RequestFormat = WebMessageFormat.Json,
    ResponseFormat = WebMessageFormat.Json,
    UriTemplate = "SaveBook/{id}")]
    string SaveBook(Book book, string id);


    I read the link:
    http://www.topwcftutorials.net/2014/02/post-json-to-wcf-restful-service.html
    http://blog.dlwelch.com/?p=390

    Thursday, July 3, 2014 12:15 AM

Answers

  • User-417640953 posted

    Hi neoaguil17,

    Thanks for your post.

    WCF rest model service is based on the Http protocol, and the Get and Post service method using the

    http protocol get and post model. Get method pass parameters append to url. Post method pass parameters in the

    http body.

    # For defining a Get rest method you can use the WebGet like below.

       [WebGet(UriTemplate = "Person/{id}")]
       public Person GetPerson(string id);

    # For defining a Get rest method you can use the WebInvoke like below.

     [WebInvoke(UriTemplate = "Person", Method = "POST")]
      public Person InsertPerson(Person person);
       

    Besides, I suggest you take a look at below article "Developing WCF Restful Services with GET and POST Methods".

    http://www.c-sharpcorner.com/UploadFile/surya_bg2000/developing-wcf-restful-services-with-get-and-post-methods/

    Hope that helps, thanks.

    Best Regards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 4, 2014 11:23 PM

All replies

  • User-484054684 posted

    It should work, did you get any issue? Hope you are passing both the parameters for your post method when calling from client code.

    Thursday, July 3, 2014 10:44 AM
  • User-417640953 posted

    Hi neoaguil17,

    Thanks for your post.

    WCF rest model service is based on the Http protocol, and the Get and Post service method using the

    http protocol get and post model. Get method pass parameters append to url. Post method pass parameters in the

    http body.

    # For defining a Get rest method you can use the WebGet like below.

       [WebGet(UriTemplate = "Person/{id}")]
       public Person GetPerson(string id);

    # For defining a Get rest method you can use the WebInvoke like below.

     [WebInvoke(UriTemplate = "Person", Method = "POST")]
      public Person InsertPerson(Person person);
       

    Besides, I suggest you take a look at below article "Developing WCF Restful Services with GET and POST Methods".

    http://www.c-sharpcorner.com/UploadFile/surya_bg2000/developing-wcf-restful-services-with-get-and-post-methods/

    Hope that helps, thanks.

    Best Regards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 4, 2014 11:23 PM