如何使用 Python 的请求将复杂类型发布到 WCF? [英] How to post complex type to WCF using Python's requests?

查看:45
本文介绍了如何使用 Python 的请求将复杂类型发布到 WCF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Python 的 request 包.

I am trying to query a WCF web service using Python's request package.

我在 WCF 中创建了一个非常简单的 Web 服务,遵循默认的 VS 模板:

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
}

从 Python 中,我设法调用前两个并轻松取回数据.

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.

这是我的python代码:

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)

但它不起作用,即,如果我在 GetDataUsingDataContract 方法中将 VS 细分,我看到 composite 参数为 null.我认为这是解析过程中的一个问题,但我看不出哪里出了问题.

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?

编辑:

这里是复杂的类型定义:

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; }
    }
}

推荐答案

您需要在 POST body 中发送 JSON,但您将其附加到查询参数中.

You need to send JSON in the POST body, but you are attaching it to the query parameters instead.

改用data,只对外部结构进行编码:

Use data instead, and only encode the outer structure:

result=req.post(wsAddr+methodPath,
                data=json.dumps({'composite': cType}),
                headers=headers)

如果您对 cType 进行编码,您将发送一个包含另一个 JSON 编码字符串的 JSON 编码字符串,该字符串又包含您的 cType 字典.

If you encoded cType, you'd send a JSON-encoded string containing another JSON-encoded string, which in turn contains your cType dictionary.

这篇关于如何使用 Python 的请求将复杂类型发布到 WCF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆