problem with PORTABLE_BYTES and python3.2

Asked by Ottavio Campana

Hi, I'm trying to realize a website that sends a binary file to a
server from the client (both client and server are ladon-based)

Thus, I used the PORTABLE_BYTES data type and tried to send a pdf.

The command I'm using on the client is

WS_client_file.new_file
(auth_username='username',auth_password='password',file_name="m.pdf",data=b'%PDF-1.6\r%\xe2\xe3\xcf\xd3\r\n1235...',
mime_type="application/pdf")

I get this error

TypeError: b'%PDF-1.6\r%\xe2\xe3\xcf\xd3\r\n1235...' is not JSON
serializable

I digged a bit in the traceback of my application and what I found is that ladon fails when it uses the json encoder from the python library. Particularly, i seems not to be able to encode the bytes data type (you can see it in the traceback, when it refers to /usr/lib/python3.2/json/encoder.py , line 170). I checked this also in python 2.7, but in that version the json encoder is able to deal with the str data type.

So my question is: did you ever see any regression with PORTABLE_BYTES and python3? Or am I making a mistake?

Thank you for your help,

Ottavio

Here's my application traceback

    File "/tmp/a/lib/python3.2/site-packages/pyramid_debugtoolbar-0.9.7-py3.2.egg/pyramid_debugtoolbar/panels/performance.py", line 55, in resource_timer_handler

    result = handler(request)

    File "/tmp/a/lib/python3.2/site-packages/pyramid-1.3a3-py3.2.egg/pyramid/tweens.py", line 20, in excview_tween

    response = handler(request)

    File "/tmp/a/lib/python3.2/site-packages/pyramid-1.3a3-py3.2.egg/pyramid/router.py", line 164, in handle_request

    response = view_callable(context, request)

    File "/tmp/a/lib/python3.2/site-packages/pyramid-1.3a3-py3.2.egg/pyramid/config/views.py", line 343, in viewresult_to_response

    result = view(context, request)

    File "/tmp/a/lib/python3.2/site-packages/pyramid-1.3a3-py3.2.egg/pyramid/config/views.py", line 426, in _requestonly_view

    response = view(request)

    File "/tmp/a/projects/myproject/myproject/views/backoffice/top.py", line 19, in index

    return render_view_to_response(None, request, name=view_to_be_called)

    File "/tmp/a/lib/python3.2/site-packages/pyramid-1.3a3-py3.2.egg/pyramid/view.py", line 77, in render_view_to_response

    return view(context, request)

    File "/tmp/a/lib/python3.2/site-packages/pyramid-1.3a3-py3.2.egg/pyramid/config/views.py", line 316, in rendered_view

    result = view(context, request)

    File "/tmp/a/lib/python3.2/site-packages/pyramid-1.3a3-py3.2.egg/pyramid/config/views.py", line 426, in _requestonly_view

    response = view(request)

    File "/tmp/a/projects/myproject/myproject/views/backoffice/file.py", line 266, in file_new

    i = eval (command)

    File "<string>", line 1, in <module>

    File "<string>", line 2, in placeholder

    File "/tmp/a/lib/python3.2/site-packages/ladon-0.6.5-py3.2.egg/ladon/clients/jsonwsp.py", line 180, in call_method

    jsonwsp_response = self.post_request(json.dumps(data))

    File "/usr/lib/python3.2/json/__init__.py", line 224, in dumps

    return _default_encoder.encode(obj)

    File "/usr/lib/python3.2/json/encoder.py", line 188, in encode

    chunks = self.iterencode(o, _one_shot=True)

    File "/usr/lib/python3.2/json/encoder.py", line 246, in iterencode

    return _iterencode(o, 0)

    File "/usr/lib/python3.2/json/encoder.py", line 170, in default

    raise TypeError(repr(o) + " is not JSON serializable")

Question information

Language:
English Edit question
Status:
Solved
For:
ladon Edit question
Assignee:
No assignee Edit question
Solved by:
jsgaarde
Solved:
Last query:
Last reply:
Revision history for this message
Best jsgaarde (jakob-simon-gaarde) said :
#1

Hi Ottavio.

I can see you are using the jsonwsp protocol, so why not use the attachment type?

http://ladonize.org/index.php/Ladon_Attachments

Best Regards
Jakob Simon-Gaarde

Revision history for this message
Ottavio Campana (ottavio) said :
#2

Are attachment supported in the last version of ladon for soap?

Do you think that the problem with bytes data type can be a bug?

I'll try attachments anyway

Thank you,

Ottavio

Revision history for this message
jsgaarde (jakob-simon-gaarde) said :
#3

Btw, there is a full example on using attachments in the official ladon-examples pack:
http://ladonize.org/files/ladon-0.6.3-examples.tar.gz
/ Jakob

Revision history for this message
jsgaarde (jakob-simon-gaarde) said :
#4

SOAP has very bad support for attachments, and I have found that SOAP actually doesn't support the attachment features that Ladon aims for.
However, it is actually possible cheat a little. If you are using a SOAP client that can add binary multiparts freely.
If you add your attachment as an application/octet-stream multipart, without mentioning the ladon.types.attachment in the service module, you can access the attachment from your service method using the kw['attachment'] key:

example:
http://paste.ubuntu.com/814263/

In short:
* Ladon handles multipart requests even though attachments are not defined for a certain method.
* Ladon takes care of attachments even though the SOAP interface does not support it.
* All you need is a client that can send a SOAP request as a multi-part request

Best Regards
Jakob Simon-Gaarde

Revision history for this message
Ottavio Campana (ottavio) said :
#5

Thanks jsgaarde, that solved my question.

Revision history for this message
Ottavio Campana (ottavio) said :
#6

I converted my code to use attachment, it works.