Create cover with api

Glenn Williams September 27, 2024

I use a C# app to hit the api.  I can't figure out how to create a cover.  Help appreciated.

 

Here's a working sample of code I use to add two labels for reference.

url = "https://api.trello.com/1/cards/629b8xxxxxb301af2?key=mykey&token=mytoken&idLabels=629b483b831bc63f9c5e686c,629b8771c46417575b984de5";

To add a cover I've tried several things such as:

 

url = "https://api.trello.com/1/cards/629b8xxxxxb301af2?key=mykey&token=mytoken&cover={\"color\",\"green\"}";

 

Rest of the code:

  var httpRequest = (HttpWebRequest)WebRequest.Create(url); httpRequest.Method = "PUT";

httpRequest.ContentType = "application/json";

var httpResponse = (HttpWebResponse)httpRequest.GetResponse();

3 answers

0 votes
Glenn Williams September 28, 2024

Maybe I'm missing something, but I've tried several times to convert this to c# syntax.  I'm clueless.

Rasmus Wulff Jensen
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 28, 2024

Here is a full C# sample:

 

string key = "todo";

string token = "todo";

string cardId = "todo";

string coverAsJson = "{\"cover\":{\"color\":\"green\"}}";

Uri uri = new Uri($"https://api.trello.com/1/cards/{cardId}?key={key}&token={token}");

 

var httpClient = new HttpClient();

await httpClient.PutAsync(uri, new StringContent(coverAsJson, Encoding.UTF8, "application/json"));

 

0 votes
Rasmus Wulff Jensen
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 27, 2024

@David Bakkers is right. The cover itself need to be a json payload and not a query-parameter

If you do not wish to write all the C# by yourself you can also check out NuGet Package: TrelloDotNet that support manipulation of cover + a lot more: https://www.nuget.org/packages/TrelloDotNet

Or get some inspiration here:

https://github.com/rwjdk/TrelloDotNet/blob/main/TrelloDotNet/TrelloDotNet/TrelloClient.Covers.cs

And here:

https://github.com/rwjdk/TrelloDotNet/blob/main/TrelloDotNet/TrelloDotNet/Model/CardCover.cs

In the source-code as covers can be quite hard to get working correctly 

Glenn Williams September 29, 2024

Yep, I found your amazing work after I posted this.  I emailed you yesterday.  Thanks!

0 votes
David Bakkers
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.
September 27, 2024

Hello @Glenn Williams 

When doing PUT requests to that endpoint, the cover parameter is a request QUERY parameter, not a request PATH parameter, so it should be into the BODY of the request

Your request should be:

PUT

https://api.trello.com/1/cards/629b8xxxxxb301af2?key=mykey&token=mytoken

BODY

{
    "cover": {
        "color": "green"
}

}

Yes, you can 'force' query parameters into being path parameters if they have a single value (like key and token) or comma separated values (like idLabels), but everything breaks if they have nested values or an array of values.

Generally speaking, for PUT and POST requests, parameters and their values should be declared in the request body, unless they are mandated to be in the request path (like id for the card's ID).

So, the best format for the request really should be:

PUT

https://api.trello.com/1/cards/629b8xxxxxb301af2

BODY

{
"key": "myKey",
"token": "myToken",

    "cover": {
        "color": "green"
}

}

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
TAGS
AUG Leaders

Atlassian Community Events