[rt-users] REST interface attachment weirdness

Tom Lahti toml at bitstatement.net
Wed Feb 25 17:15:30 EST 2009


> Now the question is, did that fix my REST interface weirdness
> as well.. probably.  It's late so I'll work on that tomorrow.

So, I've been able to figure out now how to reconstruct the binary from the
"Content:" section in the REST response for
/REST/1.0/ticket/<ticket-id>/attachments/<attachment-id>

First, you take the "Content:" part of the response only

perl: (content_string) = rest_response =~ /Content:\s+(.*)/m
ruby: content_string = rest_response.match(/Content:\s+(.*)/m)[1]

Ignore the content-transfer-encoding header; the content is _not_ base64
encoded, even if the header says it is. In the string, do a search & replace:

perl: content_string =~ s/\n\s{9}/\n/
ruby: content_string.gsub!(/\n\s{9}/,"\n")

Next, you have to chomp off the 3 carriage returns at the end of
content_string.  Lastly, do a character set conversion.  This string is in
UTF-8 (probably only since RT 3.8, older RT it will be something else);
converting it to ISO-8859-1 gets the binary back intact.  The converted
string length should equal the content-length from the REST response as a
sanity check.

Note: I have only done this with one attachment so far, which came from a
U.S. Windows system.  Whether or not the target charset of ISO-8859-1 really
ought to be Windows-1252, or whether it ought to be whatever the original
charset was when it was uploaded (which doesn't appear to be coming back
with the REST response... oh dear) are unknown to me as of yet.  I don't
have Macs to upload binaries with, or foreign localized Windows, or anything
else for that matter so I'm not sure if "always to ISO-8859-1" will work
everywhere.  Would be nice to get some developer input on that...?

I used the iconv library to do the character set conversion.

ruby: binary = Iconv.conv("ISO-8859-1","UTF-8",content_string)

-- 
-- ============================
   Tom Lahti
   BIT Statement LLC

   (425)251-0833 x 117
   http://www.bitstatement.net/
-- ============================



More information about the rt-users mailing list