Author Topic: Upload Multipart Form - Python .... UnicodeDecodeError  (Read 219 times)

Offline mbusse

  • Newbie
  • *
  • Posts: 2
  • Karma: 0
    • View Profile
Upload Multipart Form - Python .... UnicodeDecodeError
« on: July 28, 2016, 03:57:18 AM »
I try to upload da via a form to a webserver after the user send it via Scala and always get a UnicodeDecodeError.

the problem is when i try my Phytonscript it works well but when it gets started from the Scala Script i get encoding / decoding problems i have this problem nearly everytime so how to make the encoding work correctly?

this time i cant play with the values until it works because the problem ist the image i try to upload.

Code: [Select]
class MultiPartForm(object):
    """Accumulate the data to be used when posting a form."""

    def __init__(self):
        self.form_fields = []
        self.files = []
        self.boundary = mimetools.choose_boundary()

    def get_content_type(self):
        return 'multipart/form-data; boundary=%s' % self.boundary

    def add_field(self, name, value):
        """Add a simple field to the form data."""
        self.form_fields.append((name, value))

    def add_file(self, fieldname, filename, fileHandle, mimetype=None):
        """Add a file to be uploaded."""
        body =
        if mimetype is None:
            mimetype = mimetypes.guess_type(filename)[0] or 'application/octet-stream'
        self.files.append((fieldname, filename, mimetype, body))

    def __str__(self):
        """Return a string representing the form data, including attached files."""
        # Build a list of lists, each containing "lines" of the
        # request.  Each part is separated by a boundary string.
        # Once the list is built, return a string where each
        # line is separated by '\r\n'.
        parts = []
        part_boundary = '--' + self.boundary

        # Add the form fields
                'Content-Disposition: form-data; name="%s"' % name,
            for name, value in self.form_fields

        # Add the files to upload
                'Content-Disposition: file; name="%s"; filename="%s"' % (field_name, filename),
                'Content-Type: %s' % content_type,
            for field_name, filename, content_type, body in self.files

        # Flatten the list and add closing boundary marker,
        # then return CR+LF separated data
        flattened = list(itertools.chain(*parts))
        flattened.append('--' + self.boundary + '--')
        return '\r\n'.join(flattened)

here is how it is used:
Code: [Select]
with open(bild, 'r') as f:
            form = MultiPartForm()
            form.add_field("name", name)
            form.add_file("bild", fname, f)

            # Build the request
            proxy_support = urllib2.ProxyHandler({})
            opener = urllib2.build_opener(proxy_support)

            request = urllib2.Request(self._url+"myorder.php?action=upload")
            request.add_header('User-agent', 'MyOrder (http://www.blabla.bla/)')

            # THis line will CAUSE THE ERROR
            body = str(form)
            request.add_header('Content-type', form.get_content_type())
            request.add_header('Content-length', len(body))

thank you so much