1
votes

I have this webservice, based on the Pyramid framework, that accepts a multipart/form-data request.

Everything works when I send the request from cURL; however from the Android application, it fails with the following error

TypeError: must be str, not bytes

when I try to access any field : e.g. request.POST.get('smartphoneId', None)

The difference is that the Android library adds a few headers to the fields, notably the Content-Transfer-Encoding. From cURL each value is sent as follows :

--------------------------5f28b737bc4e4813

Content-Disposition: form-data; name="smartphoneId"

2

whereas from Android (with retrofit) :

--0e0a43aa-3156-48ee-a949-931dcc4eb8be

Content-Disposition: form-data; name="smartphoneId"

Content-Type: text/plain; charset=UTF-8

Content-Length: 3

Content-Transfer-Encoding: 8BIT

999

Initially the Content-Transfer-Encoding was binary ; I manually changed it to 8BIT, but I get the same error. Seems that Python can't parse / handle the content of each field.

Any idea ?

1
Like the error message says, you are receiving a byte string, but the function wants a native string. Maybe convert it with str() or revert to Python 2 if the library is not yet Py3k-compatible.tripleee
Is this Python 3? Convert from a bytes object to a str using the decode() method.cdarke
It's Python 3 indeed; the framwework should be compatible (at least they say), but maybe there are some bugs left..Eino Gourdin
It is not a bug with pyramid, its just python3, pyramid is fully python3 compatible.crooksey
If you can replicate this as a small test, or get me a full dump of the HTTP request that fails, fill out a bug report for WebOb: github.com/pylons/webob/issues I'll take a look and see if it is something that WebOb should fix.X-Istence

1 Answers

1
votes

I had the same problem (with Python 3.4). To solve the problem, I created a patch for the following module "webob/compat.py" : replace the existing patch of multi_read by the current version from python cgi 3.5. And now, it works. I will create a pr on the github repository.

See https://bugs.python.org/issue27777.

I created a pull request for webob : https://github.com/Pylons/webob/pull/281