简体   繁体   中英

Unable to use ByteArray value received

I'm developing a soap web server with spyne. I've defined the following method inside a Service class:

@rpc(String, String, String, String, String, DateTime, String, String,
         String, Integer32, ByteArray, _returns=ResponseCode)
    def add_ticket(ctx, t_id, text, cat1, cat2, cat3, date, user, service, attachment1_name, attachment1_size, attachment1_data):
        logging.info(f"Received ticket: {t_id} with category {cat1}/{cat2}/{cat3} under service {service}")

        logging.info(f"Attachment 1: {attachment1_data}")

Anyway I'm sending some simple pdf file encoded in base64 in the ByteArray field, but I'm receiving a tuple. Is there a way to mantain or even re-encode in base64 what is received?

For example, I'm sending something that start with:

JVBERi0xLjMNCiXi48/TDQoNCjEgMCBvYmoNCjw8DQovVHlwZSAvQ2F0YWxvZw0KL091dGxpb[...]

and receiving something that starts with:

(b'%PDF-1.4\n%\xc3\xa4\xc3\xbc\xc3\xb6\xc3\x9f\n2 0 obj\n<</Length 3 0 R/Filter/FlateDecode>>\nstream\nx\x9c\xad\x1aM\x8b[...]

I've read the answer in this stack overflow question: python spyne service - base64 strings come escaped but it doesn't resolve my problem. I'm not able to define the deserialization method for ByteArray.

The option 2 in the answer is not the best that I can implement because in my wsdl file to share there would be

<xs:element name="attachment1_data" type="tns:add_ticket_attachment1_dataType" minOccurs="0" nillable="true" />

that is not a feasible data type name.

Thanks in advance!

ByteArray is used to abstract away binary encoding differences in different protocols. That's why you get a sequence of bytes objects, as documented here .

If you want the base64 string, just use the Unicode type.

If you want the base64 string yet you NEED to use ByteArray, you can try using ByteArray(encoding=None) , or subclassing the protocol and overriding the byte_array_from_element function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM