How do I set a custom field item via C# using Http Request Message?
Asked by David Funni:

I am able to successfully get all the cards and custom fields from my board using:

    string qString = @"https://api.trello.com/1/boards/{boardId}/cards?customFieldItems=true&key={myKey}&token={myToken}";  using(var request = new HttpRequestMessage(new HttpMethod("GET"), qString))  {    var response = await httpClient.SendAsync(request);    String JsonString = await response.Content.ReadAsStringAsync();

but now I want to Set one of those custom fields. I've tried this but it doesn't work:

        using(var httpClient = new HttpClient())    {       using(var request = new HttpRequestMessage(new HttpMethod("PUT"), @"https://api.trello.com/1/cards/" + myCardId + "/" + myCustomFieldId + "/item?" + "key={myKey}&token={myToken}" + "{value: { text: \"New Value\"}}"))       {            // i don't know how to set the header content so i commented it out            //request.Headers.Add("content-type", "application/json");            var response = await httpClient.SendAsync(request);            String JsonString = await response.Content.ReadAsStringAsync();

Thjanks,

Dave


Answers:
author: David Funni
body: Thanks for your guy's help.

I was able to figure out my question using this website which converts Curl to C#:

<https://curl.olsh.me/>

```
private?async?void?button4_Click(object?sender,?EventArgs?e){????using(var?httpClient?=?new?HttpClient())????{????????String?querry?=?@"https://api.trello.com/1/card/"?+?myCardId?+?"/customField/"?+?myCardsCustomFieldItems_IdCustomField?+?"/item";????????using(var?request?=?new?HttpRequestMessage(new?HttpMethod("PUT"),?querry))????????{????????????request.Content?=?new?StringContent("{  \"value\": { \"text\": \"Hello, world!\" }, \"key\": \"########\",  \"token\": \"#######\" }");????????????request.Content.Headers.ContentType?=?System.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/json"); ????????????var?response?=?await?httpClient.SendAsync(request);????????????String?JsonString?=?await?response.Content.ReadAsStringAsync(); ????????????this.textBox4.Text?=?querry;????????????this.richTextBox4.Text?=?JsonString;????????}????}}
```


author: Iain Dooley
body: [@David Funni](/t5/user/viewprofilepage/user-id/3188599) you need to have the header set, why did you comment it out?

comments: author: David Funni
body: When I put that line in, I get this exception:

System.InvalidOperationException: 'Misused header name. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects.'


author: David Funni
body: I do believe I need to figure that out though as the error message I get back has my Json string all converted to some ascii representation or something like that:

Cannot PUT /1/cards/{cardId}/{CustomFieldId}/item?key={myKey}\&token={myToken}%7Bvalue:%20%7Btext:%20%22TestName%22%7D%7D


author: Iain Dooley
body: [@David Funni](/t5/user/viewprofilepage/user-id/3188599) you might find the specifics of how to format the request here:

<https://github.com/gregsdennis/Manatee.Trello>

or you could just use that lib :)


author: milynnus
body: [@David Funni](/t5/user/viewprofilepage/user-id/3188599) I code using python wrapper for Trello API so I am not familiar with c#. First Trello CF definitions are kept at the board level. So you need to use this

### Get Custom Fields for Board {#toc-hId-1174860052}

GET /1/boards/{id}/customFields

Get the Custom Field Definitions that exist on a board.

in the first part of your code to get CF definitions. As such I believe that your myCustomFieldID not the id of the CF text field you want to set.   

For the second part the API should be

### Update Custom Field item on Card {#toc-hId--632594411}

PUT /1/cards/{idCard}/customField/{idCustomField}/item

Setting, updating, and removing the value for a Custom Field on a card.

comments: author: David Funni
body: I see that I can get the custom field definitions for the board. And I do. And each one has an Id.

When I get the cards for the board I can request that it give me the card's custom field items also. So I do. And each of those has an Id and an IdCustomField. The Id is a unique Id of this field in this card, whereas the IdCustomField is equal to the corresponding custom field Id in the board.

I'm not sure which Id to use in my PUT though...


author: milynnus
body: [@David Funni](/t5/user/viewprofilepage/user-id/3188599) the equivalent in py-Trello for what you are doing is likely this:

def set_custom_field(self, value, custom_field):

in the code for PUT the definition ID is used.


Tags: c

custom-field