Python WebService客户端UTF-8和特殊字符

时间:2015-03-06 12:19:17

标签: python web-services utf-8

与suds和pysimplesoap请求一样,当我尝试发送带有一些UTF-8特殊字符的请求时,答案是ascii编解码器无法解码字节。由于SoapUI没有这样的问题,WebService会返回正确的答案。这是代码:

def check_customer_via_reference(self, reference):
    wsdl_service = Client(http://localhost:81/localService/Service.svc?wsdl)
    xml = Raw("""
                <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
                xmlns:wcf="http://localService">
                   <soapenv:Header/>
                   <soapenv:Body>
                      <wcf:GetCode>
                         <wcf:Par1>test</wcf:Par1>
                         <wcf:Code>ąęźżć</wcf:Code>
                      </wcf:GetCode>
                   </soapenv:Body>
                </soapenv:Envelope>
    """)
    result = wsdl_service.service.GetCode(__inject={'msg': xml})

该代码将返回

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc4 in position 380: ordinal not in range(128)

而SoapUI将返回

<a:Code>0</a:Code>

有什么想法吗?任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

使用Raw字符串更改unicode对象。

xml = Raw(u"""
                <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
                xmlns:wcf="http://localService">
                   <soapenv:Header/>
                   <soapenv:Body>
                      <wcf:GetCode>
                         <wcf:Par1>test</wcf:Par1>
                         <wcf:Code>ąęźżć</wcf:Code>
                      </wcf:GetCode>
                   </soapenv:Body>
                </soapenv:Envelope>
    """)