Ticket #10785 (closed defect: fixed)

Opened 4 months ago

Last modified 4 months ago

json-rpc ticket.putAttachment results in 'unicode' object has no attribute 'data'

Reported by: markuzmx Assigned to: osimons
Priority: normal Component: XmlRpcPlugin
Severity: blocker Keywords: attachment
Cc: olemis Trac Release: 1.0

Description

I'm trying to use the json-rcp ticket.putAttachment but when using it the call returns with this:

{    u'error': {    u'code': -32603,
                    u'message': u"ServiceException details : 'unicode' object has no attribute 'data'",
                    u'name': u'JSONRPCError'},
     u'id': None,
     u'result': None}

The data I'm sending is this:

{   'method': 'ticket.putAttachment',
    'params': [1359, 'add.png', 'description data here', 'b64encoded_data', True]}

I've replaced the b64encoded data with a simple string for brevity.

This results in the call to be unusable.

Attachments

Change History

01/11/13 09:05:56 changed by markuzmx

The easiest way to solve this is to check if the data argument is a string if it is then use it as it comes, if not, then try to use then check if the data member is present, and if not, then we don't know how to treat this and raise an error.

        if isinstance(data, basestring):
            attachment.insert(filename, StringIO(data), len(data))
        else:
            attachment.insert(filename, StringIO(data.data), len(data.data))

01/11/13 09:23:55 changed by markuzmx

Btw, is important to decode from base64 the data.

(in reply to: ↑ description ; follow-up: ↓ 5 ) 01/11/13 20:51:23 changed by olemis

  • keywords set to attachment.

Please mention Python version .

01/11/13 20:52:37 changed by olemis

  • summary changed from json-rcp ticket.putAttachment results in 'unicode' object has no attribute 'data' to json-rpc ticket.putAttachment results in 'unicode' object has no attribute 'data'.

(in reply to: ↑ 3 ) 01/11/13 21:02:07 changed by markuzmx

Replying to olemis:

Please mention Python version .

I'm running Python 2.6.5

01/12/13 00:21:08 changed by osimons

Both wiki.putAttachment and ticket.putAttachment expects Binary input:

        yield (None,
               ((str, int, str, str, Binary, bool),
                (str, int, str, str, Binary)),
               self.putAttachment)

Why the wiki method reference documentation says...

string ticket.putAttachment(int ticket, string filename, string description,
                        base64 data, boolean replace=True)

... I don't know. Documenting Binary as base64 does not make it obvious that JSON-RPC really needs to use the class-hinting and post the data as {"__jsonclass__": ["binary", "<base64-encoded>"]}.

Example from test shows how it is done, see for instance RpcWikiTestCase.test_attachments() test in source:/xmlrpcplugin/trunk/tracrpc/tests/wiki.py

For XML-RPC this makes it even less obvious that the input required is actually xmlrpclib.Binary type and not just the base64 string representation. Hmm, I'm sure the documentation used to say Binary and not base64. I wonder what changed and when...

Patch to fix documentation output most welcome ;-)

01/12/13 00:25:13 changed by osimons

The JsonTestCase.test_binary() test in source:/xmlrpcplugin/trunk/tracrpc/tests/json_rpc.py is a better example of how the JSON-RPC Binary works.

01/22/13 10:44:43 changed by osimons

  • status changed from new to closed.
  • resolution set to fixed.

(In [12546]) XmlRpcPlugin: Use more general DateTime and Binary terms for method documentation, seeing that the underlying implementation may differ between protocols. Closes #10785.


Add/Change #10785 (json-rpc ticket.putAttachment results in 'unicode' object has no attribute 'data')




Change Properties
Action