How to insert newline or tab using the API

I want to insert a newline and a tab in a long_text field using the API from my C# program. I have tried
, “\r\n”, “\n” and “\u21B5” but they are either rejected or do not cause a linebreak. Kind of weird cause when I look inside items I see they store it as \n. Something downstream has decided to reject control chars. How do I accomplish this?

I have had no trouble just putting a \n in the string and having it work, however, the column in the UI, without clicking on the column (or hovering over it) doesn’t display line breaks. So it looks line its missing in the UI until click into the long text column and see the full box.

Just want to rule that out before spending time looking at queries and such to see if something is haywire.

Thanks for replying - it offers some encouragement that this does work. However for me when I put \n in a string it gets rejected as an invalid token before the item is ever created.

Most likely its not being properly escaped when the query string gets serialized then. Are you using graphql variables in the query, then passing a serialized column_values object as a variable in the request?

Thanks, I will try \n, \\n, \\n, \\\n, \\\n as I found up to seven backslashes were sometimes inserted when I submitted the GraphQL query to JSON.

I’d investigate graphQL variables, because then you just serialize an object for the column values to JSON and use that string the variable value. This way you do not need to worry about escaping that string manually in any way. Let the computer do it for you.

And the magic number was 3 backslashes before the \n in my C# string:

"mutation {create_item (board_id: 5666592830, group_id: \"topics\" , item_name: \"Paragraph\" , 
column_values: \"{\\\"long_text4\\\":\\\"Line 1\\\\nLine2\\\"}\" ) { id } }"

When translated to JSON it becomes 7 backslashes before the \n:

"{\"query\":\"mutation {create_item (board_id: 5666592830, group_id: \\\"topics\\\" , item_name: \\\"Paragraph\\\" , 
column_values: \\\"{\\\\\\\"long_text4\\\\\\\":\\\\\\\"Line 1\\\\\\\\nLine2\\\\\\\"}\\\" ) { id } }\",

Having to figure out how many backslashes is pretty tedious. Hence, I will be trying your suggestion to use variables which I have not touched to this point, especially since it is already defined in the GraphQL object which is being JSONified.

This is Javascript but I hope it can assist a bit.

const query = `mutation ($boardId: ID!, $groupId: String!, $itemName: String!, $values: JSON!) {create_item (board_id: $boardId, group_id: $groupId , item_name: $itemName ,
column_values: $values){id}}`

const values = {
  [columnId]: {text: paragraphString} 
  //structure for a long_text column, varies by ColumnType
//note that in API 2023-10, many IDs have changed to ID type
//which is either String or Int. String is recommended since the API 
//returns strings
const variables = {
   boardId: "5666592830", 
   groupId: "topic",
   itemName: "Paragraph",
   values: JSON.stringify(values)

const requestBody = JSON.stringify({query, variables});
1 Like