Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

addAttachment JSON

Mustafa Abusalah July 26, 2012

I'm trying to attach a file to an existing page, the json code is below:

{"jsonrpc" : "2.0", "method" : "addAttachment", "params" : [19791898 , "{"fileName" : "image.jpg", "contentType" : "image/jpeg", "comment" : "to be ignored anyway"}" , "aBinaryObj" ], "id" : 15466905}

I got the below error:

{"id":null,"error":{"message":"The request body could not be parsed as JSON.","data":"org.codehaus.jackson.JsonParseException: Illegal unquoted character ((CTRL-CHAR, code 0)): has to be escaped using backslash to be included in string value\n at [Source: org.apache.catalina.connector.CoyoteReader@bd9c24; line: 1, column: 178]\norg.codehaus.jackson.JsonParseException: Illegal unquoted character ((CTRL-CHAR, code 0)): has to be escaped using backslash to be included in string value\n at [Source: org.apache.catalina.connector.CoyoteReader@bd9c24; line: 1, column: 178]\r\n\tat org.codehaus.jackson.JsonParser._constructError(JsonParser.java:1213)\r\n\tat org.codehaus.jackson.impl.JsonParserMinimalBase._reportError(JsonParserMinimalBase.java:375)\r\n\tat org.codehaus.jackson.impl.JsonParserMinimalBase._throwUnquotedSpace(JsonParserMinimalBase.java:341)\r\n\tat org.codehaus.jackson.impl.ReaderBasedParser._finishString2(ReaderBasedParser.java:760)\r\n\tat org.codehaus.jackson.impl.ReaderBasedParser._finishString(ReaderBasedParser.java:731)\r\n\tat org.codehaus.jackson.impl.ReaderBasedParser.getText(ReaderBasedParser.java:85)\r\n\tat org.codehaus.jackson.map.deser.BaseNodeDeserializer.deserializeAny(JsonNodeDeserializer.java:219)\r\n\tat org.codehaus.jackson.map.deser.BaseNodeDeserializer.deserializeArray(JsonNodeDeserializer.java:201)\r\n\tat org.codehaus.jackson.map.deser.BaseNodeDeserializer.deserializeAny(JsonNodeDeserializer.java:216)\r\n\tat org.codehaus.jackson.map.deser.BaseNodeDeserializer.deserializeObject(JsonNodeDeserializer.java:187)\r\n\tat org.codehaus.jackson.map.deser.BaseNodeDeserializer.deserializeAny(JsonNodeDeserializer.java:213)\r\n\tat org.codehaus.jackson.map.deser.JsonNodeDeserializer.deserialize(JsonNodeDeserializer.java:56)\r\n\tat org.codehaus.jackson.map.deser.JsonNodeDeserializer.deserialize(JsonNodeDeserializer.java:13)\r\n\tat

Any advise?

3 answers

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

2 votes
Joe Clark
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
August 12, 2012

Given that anything passed into the JSON-RPC API needs to conform to the JSON spec, it's probably not possible to pass in a byte array value to Confluence via the JSON-RPC API.

I've raised a bug report for this problem - https://jira.atlassian.com/browse/CONF-26304

In the meantime, you could try using XML-RPC to add the attachment, which will accept the byte array value as a Base64-encoded String.

Mustafa Abusalah October 9, 2012

The XML-RPC is not also working due to the old implementation of axis as some of confluence methods are overloaded and the new version of axis does not allow overloading so I'm failing to use it as well.

0 votes
Benjamin Scarola July 30, 2013

FWIW I've got a JSON-RPC addAttachment call working from the browser via ajax. You have to Base64 encode the binary content ( even if it is just text ). We're using the native btoa() method provided by modern browsers. If you need legacy IE support you'll have to roll your own encoder. Also be careful that if your text includes unicode you'll need to do some additional swizzling before you Base64 encode. Something like this:

window.btoa(unescape(encodeURIComponent( str )));

See here for more details on why you need to do this:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Base64_encoding_and_decoding#The_.22Unicode_Problem.22

Andrew Stern July 28, 2014

I've tried doing this with a random JPG file using the FileReader.readAsDataURL method but I get an "unable.to.parse.method.arguments" error. Did you encounter this? And if so, how did you work around it?

0 votes
Igor Loskutov
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 27, 2012

It looks likey your string has some non-printable char with code 0 that not allowed by jackson. Check column 178 try write it again from keyboard without any copy-paste

Mustafa Abusalah July 30, 2012

I don't see any improper charachter at all. Have you ever used this method successfuly? Can you please give me a code example?

How do u sent the attachments? I convert it to binary.

Igor Loskutov
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 30, 2012

I didn't used that method at all, just suggested. It can be binary object problem too if it use some non-printable characters in encoded form. What encoding do you use for binary object serialisation?

Convenient method for that is http://en.wikipedia.org/wiki/Base64

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events