[英]how to send SOAP request including a Base64 encoded .csv file with python requests?
I'm trying to send a SOAP request with python requests module, where part of the SOAP Body contains a Base64 encoded.csv file. I'm trying to send a SOAP request with python requests module, where part of the SOAP Body contains a Base64 encoded.csv file. The basic request is working.
基本请求有效。 The file is attached as a Base64-encoded string inside an arbitrary tag inside
<soapenv:Body>
.该文件作为 Base64 编码的字符串附加在
<soapenv:Body>
内的任意标记内。
The SOAP service is a facade for an internal email service, which I also maintain. SOAP 服务是内部 email 服务的外观,我也维护它。 I know it's working because I'm getting the email, and it has an attachment, but the attached file is garbled, for lack of a better word.
我知道它正在工作,因为我得到的是 email,它有一个附件,但附件是乱码,因为没有更好的词。
I'm fairly new to python, so plenty of this could be bad code, not pythonic, etc.我对 python 还很陌生,所以很多这可能是错误的代码,而不是 pythonic 等。
But most of this code I've copied from another developer, and the rest is working, except for the file attachment piece.但是我从另一个开发人员那里复制了大部分代码,并且 rest 正在工作,除了文件附件。
Using python 3.8 and requests 2.26.0使用 python 3.8 并请求 2.26.0
I'm reading the.csv from a local file with the following:我正在从本地文件中读取.csv,其中包含以下内容:
file = open('myfile.csv', 'rb')
file_content = file.read()
base64_data = base64.b64encode(file_content)
I also tried using base64.encodebytes(file_content)
with similar results of garbled characters, although they were slightly different.我还尝试使用
base64.encodebytes(file_content)
得到类似的乱码结果,尽管它们略有不同。 Not sure what the difference in these methods is, but one of them appears to have many more lines, and the other appeared to have fewer lines, which I think is right, as it's a small file with only a few lines.不知道这些方法的区别是什么,但其中一个似乎有更多的行,而另一个似乎有更少的行,我认为这是正确的,因为它是一个只有几行的小文件。
then creating the request with the following:然后使用以下内容创建请求:
def _createRequest(self, toemail, emailsubject, emailbody, file_to_attach):
soap_request = """<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Header>
...
</soap:Header>
<soap:Body>
<EmailRequest xmlns="...">
<AddressTo>
{to_email}
</AddressTo>
<From>blah</From>
<Subject>{subject}</Subject>
<Body><![CDATA[
{body}
]]></Body>
<BinaryAttachment filename="myfile.csv" MediaServer="N">{file_base64_encoded}</BinaryAttachment>
<ContentType>HTMLText</ContentType>
</EmailRequest>
</soap:Body>
</soap:Envelope>""".format(to_email=toemail, subject=emailsubject, body = emailbody, file_base64_encoded=file_to_attach)
return soap_request
I wonder if it has something to do with the headers.我想知道它是否与标题有关。 We are using Basic auth in the headers with this:
我们在标题中使用基本身份验证:
def __getBasicAuthHeaders(self, user_name, password):
authHeader = base64.b64encode(
(user_name + ':' + password).encode()).decode()
return {
'Authorization': 'Basic ' + authHeader,
'Content-Type': 'application/xml'
}
By any chance are you sending base64_data
as a parameter to the _createRequest
function?您是否有机会将
base64_data
作为参数发送到_createRequest
function? From your code you seem to expect a string, but you get bytes there.从您的代码中,您似乎期望一个字符串,但您在那里得到字节。 For example:
例如:
>>> file = open('test.txt', 'rb')
>>> file_content = file.read()
>>> base64_data = base64.b64encode(file_content)
>>>
>>> soap_request1 = """<BinaryAttachment>{file_base64_encoded}</BinaryAttachment>""".format(file_base64_encoded=base64_data)
>>> soap_request1
"<BinaryAttachment>b'Tm90aGluZyB0byBzZWUgaGVyZSE='</BinaryAttachment>"
>>>
>>> soap_request2 = """<BinaryAttachment>{file_base64_encoded}</BinaryAttachment>""".format(file_base64_encoded=base64_data.decode())
>>> soap_request2
'<BinaryAttachment>Tm90aGluZyB0byBzZWUgaGVyZSE=</BinaryAttachment>'
>>>
Look at the differences between soap_request1
and soap_request2
and you might find your culprit.查看
soap_request1
和soap_request2
之间的区别,您可能会找到罪魁祸首。 Also have a look at this: Convert bytes to a string也看看这个: 将字节转换为字符串
Use whatever data encoding you need, most probably utf-8.使用您需要的任何数据编码,很可能是 utf-8。
You also wondered if the Basic auth has something to do with this.您还想知道基本身份验证是否与此有关。 It doesn't, but those
encode()
/ decode()
should have provided a clue.它没有,但那些
encode()
/ decode()
应该提供了一个线索。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.