I am trying to query a WCF web service using Python's request package.
I created a very simple web service in WCF, following the default VS template:
[ServiceContract]
public interface IHWService
{
[OperationContract]
[WebInvoke(Method="GET", UriTemplate="SayHello", ResponseFormat=WebMessageFormat.Json)]
string SayHello();
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "GetData", ResponseFormat = WebMessageFormat.Json)]
string GetData(int value);
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "GetData2", BodyStyle=WebMessageBodyStyle.Bare, RequestFormat=WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
CompositeType GetDataUsingDataContract(CompositeType composite);
// TODO: Add your service operations here
}
From Python, I manage to call the first two and get the data back easily.
However, I am trying to call the third one, which adds the concept of complex types.
This is my python code:
import requests as req
import json
wsAddr="http://localhost:58356/HWService.svc"
methodPath="/GetData2"
cType={'BoolValue':"true", 'StringValue':'Hello world'}
headers = {'content-type': 'application/json'}
result=req.post(wsAddr+methodPath,params=json.dumps({'composite':json.dumps(cType)}),headers=headers)
But it does not work, i.e., if I put a breakdown in VS in the GetDataUsingDataContract
method, I see that the composite
argument is null
. I think this comes from a problem in parsing, but I can't quite see what's wrong.
Do you see an obvious mistake there?
Do you know how I can debug inside the parsing mechanism?
EDIT:
Here is the complex type definition:
[DataContract]
public class CompositeType
{
bool boolValue = true;
string stringValue = "Hello ";
[DataMember]
public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
}
[DataMember]
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
}