我正在使用 zeep 与 Python 中的 Web 服务交互;我的一切都基本正常,但在设置某些字段时遇到困难。问题似乎与输入字符串有关,它尝试将其转换为“文本”但失败。我有点迷失,不知道如何实现这一点,到目前为止我还没有运气。我假设我必须进行某种转换,但我不确定如何做到这一点。
对于上下文,这就是我想要设置的。已找到字段(本例中为优惠券 ID),但 FieldValue 的格式不正确。这是该代码的概要。
#Set fields
voucher_id.FieldId = 15 #Voucher ID
voucher_id.FieldValue = "test"
#Array
field_array = factory.ArrayOfFieldWithValue()
field_array['FieldWithValue'].append(voucher_id)
#Call service function to add document info, which causes the exception
这里是异常消息。
#Post envelope
<faultstring>Server was unable to read request. ---> There is an error in XML document (2, 253). ---> Input string was not in a correct format.</faultstring>
<detail>
<ExceptionType>System.InvalidOperationException</ExceptionType>
<ExceptionMessage>System.InvalidOperationException: There is an error in XML document (2, 253). ---> System.FormatException: Input string was not in a correct format.
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReader1.Read135_SetMetadata()
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
#Response envelope
<faultstring>Server was unable to process request. ---> Voucher ID cannot be converted to Text</faultstring>
<detail>
<ExceptionType>FileHold.Common.ArgumentException</ExceptionType>
<ExceptionMessage>FileHold.Common.ArgumentException: Voucher ID cannot be converted to Text
at FileHold.LibraryManager.FieldDefinition.Validate(Object value, Int32 schemaId) in c:\Builds\5\FileHold\FileHold16\src\FileHold\LibraryManager\Code\FieldDefinition.cs:line 269
at FileHold.LibraryManager.DocumentManager.MergeAndValidateValues(FieldWithValue[] fieldsWithValues, FieldDefinition[] currentFieldDefinitions, DocumentData& currentDocumentData, Boolean canEditReadOnly, Boolean initializeMode, List`1& changedFieldsIds) in c:\Builds\5\FileHold\FileHold16\src\FileHold\LibraryManager\DocumentManager.asmx.cs:line 135
at FileHold.LibraryManager.DocumentManager.AddDocumentInfo(DocumentInfo info, Database database, DbTransaction transaction) in c:\Builds\5\FileHold\FileHold16\src\FileHold\LibraryManager\DocumentManager.asmx.cs:line 1134
at FileHold.LibraryManager.DocumentManager.AddDocumentInfo(DocumentInfo info) in c:\Builds\5\FileHold\FileHold16\src\FileHold\LibraryManager\DocumentManager.asmx.cs:line 873</ExceptionMessage>
<ErrorCode>InvalidFormat</ErrorCode>
</detail>
</soap:Fault>
</soap:Body>
</soap:Envelope>
我尝试以其他几种基本方式格式化 FieldValue,但总是收到相同的错误。
通过使用zeep.xsd.AnyObject将Python类型转换为xsd类型解决。
voucher_id.FieldValue = zeep.xsd.AnyObject(zeep.xsd.String(), 'zeep_test')