Why doesn’t this simple code POST data to my service:
import requests
import json
data = {"data" : "24.3"}
data_json = json.dumps(data)
response = requests.post(url, data=data_json)
print response.text
And my service is developed using WCF like this :
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/test", ResponseFormat =
WebMessageFormat.Json,RequestFormat=WebMessageFormat.Json)]
string test(string data );
Note: If is remove the input parameter data everything works fine, what may be the issue.
You need to set the content type header:
If I set
urltohttp://httpbin.org/post, that server echos back to me what was posted:If you are using
requestsversion 2.4.2 or newer, you can leave the JSON encoding to the library; it’ll automatically set the correct Content-Type header for you too. Pass in the data to be sent as JSON into thejsonkeyword argument: