Here is my json:
byte[] attachmentBytes = ZipToBase64();
string json = "{ \"method\": \"Bug.add_attachment\", " +
" \"params\": [ {" +
" \"ids\": " + " \"" + "25" +"\", " +
" \"data\": " + " \"" + attachmentBytes + "\", " +
" \"file_name\": " + " \"BugReport.zip\", " +
" \"Bugzilla_login\": " + " \"mymail@hotmail.com\", " +
" \"Bugzilla_password\": " + " \"mypassword\", " +
" \"summary\": " + " \"blah blah\", " +
" \"content_type\": " + " \"application/octet-stream\" " +
" } ], " +
"\"id\": 1"
+ "}";
public static byte[] ZipToBase64()
{
string filePath = @"C:\Users\John\Desktop\SomeArchive.zip";
if (!string.IsNullOrEmpty(filePath))
{
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
byte[] filebytes = new byte[fs.Length];
fs.Read(filebytes, 0, Convert.ToInt32(fs.Length));
string encodedData = Convert.ToBase64String(filebytes, Base64FormattingOptions.InsertLineBreaks);
string encoded = encodedData;
return filebytes;
}
return null;
}
I think the problem is in the attachmentBytes part, since it is a byte[]. How to pass byte[] in json?
The code is in C#.
It seems you’re trying to concatenate a byte[] to a string. That won’t work. I’m guessing the error you’re getting is a compile error about this concatenation, right?
Instead of returning the byte[], return the base64 string that
Convert.ToBase64Stringreturns. You can embed that string in your JSON.While we’re on the subject, you can drastically shorten your file reading by simply calling
File.ReadAllBytes(filePath), which encapsulates all the filestream business so you won’t have to.