Like POST with parameters using curl for WCF service

How to use command line tool curlto publish to WCF service with more than one parameter?

I have a service like the following

[ServiceContract]
public interface IMyService
{
    [OperationContract]
    [WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped)]
    void PostMethod(string varString, bool varBool);
}

And I configure the server as follows

var service = new WebServiceHost(typeof(MyService),
    new Uri("http://localhost:3000/MyService");
service.AddServiceEndpoint(typeof(IMyService), new WebHttpBinding(), "");
service.Open();

How do I call this method through curl?

curl -d varString=foo -d varBool=true http://localhost:3000/MyService/PostMethod

Where is the mistake? Is the BodyStyle method incorrect? Should I wrap the parameters in the [DataContract] class as follows?

[DataContract]
public class PostMethodResult
{
    [DataMember]
    public string varString { get; set; }

    [DataMember]
    public bool varBool { get; set; }
}
+3
source share
1 answer

Revised answer:

The URI pattern will not work for using curl to send a request to a web service. WebInvoke expects the POST body to be either XML or JSON.

WebMessageBodyStyle.Bare does not work because you have 2 parameters, so you need to wrap.

[OperationContract]
[WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped)]
string PostMethod(string varString, bool varBool); 

:

 <PostMethod  xmlns="http://tempuri.org/"><varString>1</varString><varBool>true</varBool> </PostMethod>

UriTemplate, URI, POST, .

, , - JSON

[OperationContract]
[WebInvoke(BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json)]
string PostMethodJson(string varString, bool varBool);

, :

curl -d"{\"varString\":\"so99\",\"varBool\":\"true\"}"  -i -X POST -H "Content-Type:application/json"  http://localhost:3000/MyService/PostMethodJson
+1

All Articles