0

I'm making a GET method, call it with link and everything is ok. Here it is:

    [OperationContract]
    [WebInvoke(
        Method = "GET",
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json,
        UriTemplate = "/myMethod/{input}",
        BodyStyle = WebMessageBodyStyle.Bare
        )]
    MyClass myMethod(string input);

And here is the url with whom I'm calling it: http://localhost:1234/Service1.svc/json/myMethod/blabla

However, when I'm making a post method, it doesn't work. Here is my POST method:

    [OperationContract]
    [WebInvoke(
        Method = "POST",
        ResponseFormat = WebMessageFormat.Json,
        RequestFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Bare
        )]
    MyClass anotherMethod(string comeOn);

and I call it with this link: http://localhost:1234/Service1.svc/json/anotherMethod?comeOn=smthing and it says Method not allowed.

How do I call a POST method?

petko_stankoski
  • 10,459
  • 41
  • 127
  • 231

2 Answers2

2

Ok so the diff between a POST and a GET is that the GET you have all the params on the query string, whereas a POST has the params inside the message body of the request.

http://wiki.answers.com/Q/What_is_the_difference_between_get_and_post_method_in_HTTP

Your second call is actually doing a GET hence why it's not allowing that method.

A way to test the post can be found here.

How to simulate browser HTTP POST request and capture result in C#

Community
  • 1
  • 1
Dave Walker
  • 3,498
  • 1
  • 24
  • 25
  • That means that every method for which my client needs to enter the input, the method will be GET right? – petko_stankoski Dec 24 '11 at 11:43
  • No it should be a post. If you issue the request with all the params on the QS then it will be a GET. The HTTP methods are quite important for good restful design. With POST because the params are in the method body there is a much higher content limit that can be sent to the service. Is that what oyu are meaning? – Dave Walker Dec 24 '11 at 11:49
  • @rangitaz But if the POST methods' parameters are in the code, that means that the user can't enter them, right? And that means that every method which need the user to enter an input is GET. – petko_stankoski Dec 24 '11 at 11:52
  • 1
    Ok so how are users calling these? If users are issuing the requests via a browser or something then yes it will be a GET. If they are filling in a form on a client and the client is posting the data via the service then it can be a POST. Here is an example of a JQuery client posting data http://stackoverflow.com/questions/230401/how-to-use-jquery-to-call-an-asp-net-web-service#answer-485536 – Dave Walker Dec 24 '11 at 12:00
0

Here's a link to MSDN on working with REST web services.

http://msdn.microsoft.com/en-us/netframework/cc950529.aspx

Tomislav Markovski
  • 12,331
  • 7
  • 50
  • 72