I'm trying to send an image to trello rest api, and pass mimetype and filename along using WebClient.
In python I can do this with the following function:
resp = requests.post("https://trello.com/1/cards/{}/attachments".format(trelloCardID),
params={'key': key, 'token': token},
files={'file': (theName, imgdata, theMIMEtype)})
Where theName is a filename, imgdata is the file an theMIMEtype is the mimetype.
In c# I get close with WebClient but I can't figure out how to pass the mimetype in the same way.
I'm passing this parameter along with the file, but here name and mimetype has separate entries.
myNameValueCollection.Add("token", token);
myNameValueCollection.Add("key", key);
myNameValueCollection.Add("name", "testName.png");
myNameValueCollection.Add("mimetype", "image/png");
myNameValueCollection.Add("file", base64Decoded);
I could create a separate NameValueCollection for the filename, file and mimetype, but NameValueCollection only accepts key,value pairs.
Any help is very much appreciated.
For now trello api doesn't recognize the filetype.
This is my c# code:
using Newtonsoft.Json;
using System;
using System.Collections.Specialized;
using System.Net;
using System.Text;
namespace RescoTrello
{
class Program
{
static void Main(string[] args)
{
// Create a new WebClient instance.
WebClient myWebClient = new WebClient();
//Define Token, key and url
string key = "xxxx";
string token = "yyyy";
var url = string.Format("https://api.trello.com/1/cards/{0}/attachments", "zzzz");
// Get file containing base64 encoded string from filesystem and convert it to a file
string fileName = "C:/file.txt";
string base64Encoded = System.IO.File.ReadAllText(fileName);
byte[] data = System.Convert.FromBase64String(base64Encoded);
string base64Decoded = System.Text.ASCIIEncoding.ASCII.GetString(data);
//Build NameValueCollection to send along with the request
NameValueCollection myNameValueCollection = new NameValueCollection();
myNameValueCollection.Add("token", token);
myNameValueCollection.Add("key", key);
myNameValueCollection.Add("name", "testName.png");
myNameValueCollection.Add("mimetype", "image/png");
myNameValueCollection.Add("file", base64Decoded);
//Send the request
try
{
var result = myWebClient.UploadValues(url, "POST", myNameValueCollection);
//Convert result to json and further to object model
Rootobject resultJson = JsonConvert.DeserializeObject<Rootobject>(Encoding.ASCII.GetString(result));
Console.WriteLine(resultJson.id);
}
catch (WebException e)
{
Console.WriteLine(e.Message);
}
}
}
}