【问题标题】:How to pass byte data in the xml payload如何在 xml 有效负载中传递字节数据
【发布时间】:2022-12-18 04:12:42
【问题描述】:

我正在尝试将 xml 有效载荷作为输入传递给函数并在有效载荷中对象压缩数据字段不将字符串作为输入,而是需要输入为字节格式。

def getSoapResponse(Envelope, url, action):
    try:
        envelope = Envelope
        # Create and register opener. Requires proxy when behind a firewall
        opener = urllib.request.build_opener(urllib.request.HTTPHandler(), urllib.request.HTTPSHandler(),
                                             urllib.request.ProxyHandler())
        urllib.request.install_opener(opener)
        # Create request for the service call
        request = urllib.request.Request(url)

        # Configure the request content type to be xml
        request.add_header("Content-Type", 'application/soap+xml;charset=utf-8')

        # Set the SOAP action to be invoked; while the call works without this, the value is expected to be set based on standards
        request.add_header("SOAPAction", action)

        # Write the xml payload to the request
        request.data = envelope.encode()
        
        handle = urllib.request.urlopen(request, cafile=certifi.where())
            
        # Get the response and process it
        xmlString = (handle.read(2000).decode('utf-8'))
        if action != "uploadObjectInSession":
            # Convert the response into XML tree structure
            sessionxml = fromstring(xmlString)
            # Get the session value from the XML tree
            ReturnVal = sessionxml[0][0][0].text if action == 'login' else 'Success'
            return (ReturnVal)
        else:
            return "Success"
    except Exception as err:
        print("getSoapResponse - Error occurred; Message ", str(err))
        print("===============================================================")
        return "Error"

def uploadSession(catFile, Target):
    try:
       with open(catFile, 'rb') as f:
           bytes_encoded = base64.b64encode(f.read())
           string_encoded = bytes_encoded.decode()
           f.close()
    except IOError:
       return "Error"

new_Envelope = """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v2="http://xmlns.oracle.com/oxp/service/v2">
        <soapenv:Header/>
        <soapenv:Body>
            <v2:uploadObjectInSession>
                <v2:reportObjectAbsolutePathURL>""" + Target + """</v2:reportObjectAbsolutePathURL>
                <v2:objectType>xdoz</v2:objectType>
                <v2:objectZippedData>""" + string_encoded + """</v2:objectZippedData>
                <v2:bipSessionToken>""" + target_sessionid + """</v2:bipSessionToken>
            </v2:uploadObjectInSession>
        </soapenv:Body>
        </soapenv:Envelope> """

Final = getSoapResponse(Envelope=new_Envelope, url, action='uploadObjectInSession')
    if Final=="Error":
       print("FAILURE")
       return "Error"
    else:
       print(Final)
       return "Success"

调用上述函数后,如果我将字符串传入对象压缩数据,它没有成功,而如果我传递字节数据对象压缩数据,我收到以下错误

new_Envelope = """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v2="http://xmlns.oracle.com/oxp/service/v2">
TypeError: can only concatenate str (not "bytes") to str

【问题讨论】:

    标签: python python-3.x string oracle oracle-fusion-apps


    【解决方案1】:

    使用.encode()将字符串转换为字节

    >>> mystr="test"
    >>> type(mystr)
    <class 'str'>
    >>> type(mystr.encode())
    <class 'bytes'>
    

    你的错误表明,当通过连接(使用+)将一堆字符串连接在一起创建你的newEnvelope时,其中一个实际上[已经]是bytes类型。

    找到哪个并使用.decode() 将其转换为字符串。

    【讨论】:

    • 我能够从字符串转换为字节,但我需要帮助将字节数据传递到有效载荷中对象压缩数据场地
    • 将它替换为 string_encoded 时是否出现错误?
    • “string_encoded”是结果的字符串版本,“byte_encoded”是结果的字节版本。当我传递 string_encoded 时收到不成功的响应,因为它只接受字节输入。
    • 所以你是说 getSoapResponse() 期望 bytes 得到 new_envelope 的内容?
    • 正确的!为了对象压缩数据场地。
    猜你喜欢
    • 1970-01-01
    • 2017-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多