Modify

Opened 11 years ago

Closed 11 years ago

#10785 closed defect (fixed)

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

Reported by: Marco Antonio Islas Cruz Owned by: osimons
Priority: normal Component: XmlRpcPlugin
Severity: blocker Keywords: attachment
Cc: Olemis Lang 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 (0)

Change History (8)

comment:1 Changed 11 years ago by Marco Antonio Islas Cruz

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))

comment:2 Changed 11 years ago by Marco Antonio Islas Cruz

Btw, is important to decode from base64 the data.

comment:3 in reply to:  description ; Changed 11 years ago by Olemis Lang

Keywords: attachment added

Please mention Python version .

comment:4 Changed 11 years ago by Olemis Lang

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

comment:5 in reply to:  3 Changed 11 years ago by Marco Antonio Islas Cruz

Replying to olemis:

Please mention Python version .

I'm running Python 2.6.5

comment:6 Changed 11 years ago 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 ;-)

comment:7 Changed 11 years ago 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.

comment:8 Changed 11 years ago by osimons

Resolution: fixed
Status: newclosed

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

Modify Ticket

Change Properties
Set your email in Preferences
Action
as closed The owner will remain osimons.
The resolution will be deleted. Next status will be 'reopened'.

Add Comment


E-mail address and name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.