Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use both GET and POST in a same method?

Tags:

rest

c#

wcf

I would like to know, in a wcf rest service is it possible to use both HTTP POST and HTTP Get in same method? I mean to say that a client page can either use post or get to invoke my method.

My client wants me to implement a method in this way.

As our API is a "RESTful" service we should be able to use both GET and POST with this method. The parameter can be placed in the URL of a GET request and also in the Header section of the GET request. When using a HTTP POST with this method the parameter can be stored in the header section or the body.

Is it possible?

like image 791
Arti Avatar asked Oct 21 '22 15:10

Arti


1 Answers

Lets do it by writing code! Suppose you have a method!

[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "Leads",
ResponseFormat = WebMessageFormat.Xml,
RequestFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare)]
[WebGet(UriTemplate="/Leads")]
Result AddLeads(ReqLead[] rl); // This is our method.

When you will run your service, it will compiled and run successfully and browser will display you a page.

Service

Now green arrow pointing is my Service name. By cliking on it will redirect you an error page and it will make an idea clear to you! See the image below.

Error

I hope it will give you an idea. Moreover! In you [OperationContract] you will define only one method type, whether GET or POST. You can't have both.

Thanks

like image 169
Faizan Mubasher Avatar answered Oct 26 '22 23:10

Faizan Mubasher