在python中为soap请求添加标题和正文作为dict

2024-10-03 17:28:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用python请求库发出soap请求,这就是我正在做的事情及其工作。在这里,我以xml的形式发送正文,如何使用dict而不是xml做同样的事情

import requests 
    headers = {"content-type":"text/xml"}
    body='''
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1="http://www.tmforum.org/mtop/fmw/xsd/hdr/v1" xmlns:v11="http://www.tmforum.org/mtop/mri/xsd/mer/v1" xmlns:v12="http://www.tmforum.org/mtop/fmw/xsd/nam/v1">
       <soapenv:Header>
          <v1:header>
             <v1:security>testuser:test@123</v1:security>
             <v1:communicationPattern>MultipleBatchResponse</v1:communicationPattern>
          </v1:header>
       </soapenv:Header>
       <soapenv:Body>
          <v11:getAllTestRequest>
             <v11:tdOrTmnRef>
                <v12:tdn>
                   <v12:type>td</v12:type>
                   <v12:value>Post/t3400</v12:value>
                </v12:tdn>
             </v11:tdOrTmnRef>
          </v11:getAllTestRequest>
       </soapenv:Body>
    </soapenv:Envelope>
    '''

    req = requests.post(url, data=body, headers=headers)
    print (req.content)

例如,这里我想以dict而不是xml的形式发送数据

header={"content-type":"text/xml", "security":"testuser:test@123"}
body = {"type":"td", "value":"Post/t3400"}

我可以这样做吗


Tags: orghttpwwwtypebodyxmlcontentheaders