Note that there are some explanatory texts on larger screens.

plurals
  1. POascii error in python serializing a file for SOAP
    primarykey
    data
    text
    <p>I'm working on a soap wrapper for an API (i know REST exists... it's a project in my job), I'm using SUDS library for that.</p> <p>I found <a href="https://stackoverflow.com/questions/6601107/how-to-send-a-file-through-soap-in-python">this</a> question and the answer helped me a lot. After trying a couple of things and modified a little bit the script where that question pointed me I got the following error:</p> <pre><code>UnicodeDecodeError: 'ascii' codec can't decode byte 0x82 in position 2159: ordinal not in range(128) </code></pre> <p>This occurs when sending the request to the SOAP endpoint, I tried to use unicodedata with the NFKD option to normalize it but I still getting the same erros.</p> <p>Since this is not a simple text, this is actual data from an audio file, I'm not sure but I guess that modifying it should corrupt the actual data, so I don't know what to do here.</p> <p>This is what I'm doing, this isn't too different from the procedure in the other question:</p> <pre><code>file = open('path/to/the/file.mp3', 'rb') mime_type = 'audio/mpeg' audio_data = file.read() bin_param = (audio_data, uuid.uuid4(), mime_type) with_soap_attachment(client.service.set_greeting, bin_param, request_data) </code></pre> <p>The script as far as I understood, transforms all the input arguments into a SOAP message string and then encapsulates it into a Request object and send it to the SOAP endpoint, so, the problem is that audio_data contains invalid ascii characters.</p> <p>Any clue here?</p> <p><strong>EDIT: 04/04/2013</strong></p> <p>Here is the actual code of the wrapper</p> <pre><code>#coding: utf-8 from suds.transport import Request import re import uuid def with_soap_attachment(suds_method, attachment_data, soap_location, *args, **kwargs): MIME_DEFAULT = 'text/plain' attachment_transfer_encoding = 'binary' soap_method = suds_method.method if len(attachment_data) == 3: data, attachment_id, attachment_mimetype = attachment_data elif len(attachment_data) == 2: data, attachment_mimetype = attachment_data attachment_id = uuid.uuid4() elif len(attachment_data) == 1: data = attachment_data attachment_mimetype = MIME_DEFAULT attachment_id = uuid.uuid4() soap_client = suds_method.clientclass(kwargs) binding = soap_method.binding.input soap_xml = binding.get_message(soap_method, args, kwargs) boundary_id = 'uuid:%s' % uuid.uuid4() root_part_id ='uuid:%s' % uuid.uuid4() request_headers = { 'Content-Type': '; '.join([ 'multipart/related', 'type="text/xml"', 'start="&lt;%s&gt;"' % root_part_id, 'boundary="%s"' % boundary_id, ]), } soap_headers = '\n'.join([ 'Content-Type: text/xml; charset=UTF-8', 'Content-Transfer-Encoding: 8bit', 'Content-Id: &lt;%s&gt;' % root_part_id, '', ]) attachment_headers = '\n'.join([ 'Content-Type: %s' % attachment_mimetype, 'Content-Transfer-Encoding: %s' % attachment_transfer_encoding, 'Content-Id: &lt;%s&gt;' % attachment_id, '', ]) request_text = '\n'.join([ '', '--%s' % boundary_id, soap_headers, unicode(soap_xml), '--%s' % boundary_id, attachment_headers, data, '--%s--' % boundary_id ]) location = soap_location headers = suds_method.client.options.headers.copy() headers.update(request_headers) request = Request(location, request_text) request.headers = headers response = suds_method.client.options.transport.send(request) return response </code></pre> <p>Here is the entire traceback</p> <pre><code>--------------------------------------------------------------------------- UnicodeDecodeError Traceback (most recent call last) /home/israelord/.virtualenvs/ringtu-env/local/lib/python2.7/site-packages/django/core/management/commands/shell.pyc in &lt;module&gt;() ----&gt; 1 soap_attachments.with_soap_attachment(service.set_menu_prompt, bin_param, AUTOATTENDANT_ENDPOINT, request) /home/israelord/Work/4geeks/ringtu/ringtu/services/soap_attachments.py in with_soap_attachment(suds_method, attachment_data, soap_location, *args, **kwargs) 54 attachment_headers, 55 data, ---&gt; 56 '--%s--' % boundary_id 57 ]) 58 UnicodeDecodeError: 'ascii' codec can't decode byte 0x82 in position 148: ordinal not in range(128) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload