Modify

Opened 2 years ago

Closed 2 years ago

#10785 closed defect (fixed)

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

Reported by: markuzmx Owned by: 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 (0)

Change History (8)

comment:1 Changed 2 years ago 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))

comment:2 Changed 2 years ago by markuzmx

Btw, is important to decode from base64 the data.

comment:3 in reply to: ↑ description ; follow-up: Changed 2 years ago by olemis

  • Keywords attachment added

Please mention Python version .

comment:4 Changed 2 years ago 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'

comment:5 in reply to: ↑ 3 Changed 2 years ago by markuzmx

Replying to olemis:

Please mention Python version .

I'm running Python 2.6.5

comment:6 Changed 2 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 2 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 2 years ago by osimons

  • Resolution set to fixed
  • Status changed from new to closed

(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 Comment

Modify Ticket

Action
as closed The owner will remain osimons.
The resolution will be deleted. Next status will be 'reopened'.
Author


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

 
Note: See TracTickets for help on using tickets.