C# query escape characters- 500 error

Hi,

I seem to be struggling with a very simple query that I can’t seem to get to execute without giving me a 500 error.

Below using test query everything executes as normal. When I try to execute a query with quotation marks in the query and use the escape characters the app returns 500 error.

I have tried removing the escape characters and obviously that throws an error. Any insight as to what this issue could be would be much appreciated.

var testQuery = "{\"query\":\" {boards(ids:5146042481) {id}}\"}";
var fullQuery = "{\"query\":\" {items_page_by_column_values(board_id: 5146042481 columns: [{column_id: \"name\", column_values: \"DDNL05\"}]) {cursor items{id}}} \"}";

The below code does work in the API playground as well:

{
  items_page_by_column_values(
    board_id: 5146042481
    columns: [{column_id: "name", column_values: "DDNL05"}]
  ) {
    cursor
    items {
      id
    }
  }
}

The problem is you are trying to manually serialize your request body.

# warning, I am not a C# guy, so I know C# gets weird about strings. Needing a number of 
# $ equal to the number of sequential {{{ for example, and """ three or greater depending 
# on how many sequential quotes are inside the string.
var queryString = """{
  items_page_by_column_values(
    board_id: 5146042481
    columns: [{column_id: "name", column_values: "DDNL05"}]
  ) {
    cursor
    items {
      id
    }
  }
}""";
var fullQuery = JsonSerializer.Serialize({query: queryString});

Investigate GraphQL Variables because they will make your life much easier trying to substitute the values in your query.

1 Like

Hello @J_Morl I don’t have experience with C# either, but this might help. I used your query in Postman and then exported it and this is the result:

C# HttpClient:

var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, "https://api.monday.com/v2/");
request.Headers.Add("API-Version", "2023-10");
request.Headers.Add("Authorization", "APITOKEN");
var content = new StringContent("{\"query\":\"{\\n  items_page_by_column_values(\\n    board_id: 1234567890\\n    columns: [{column_id: \\\"name\\\", column_values: \\\"DDNL05\\\"}]\\n  ) {\\n    cursor\\n    items {\\n      id\\n    }\\n  }\\n}\",\"variables\":{}}", null, "application/json");
request.Content = content;
var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
Console.WriteLine(await response.Content.ReadAsStringAsync());

C# RestSharp:

var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, "https://api.monday.com/v2/");
request.Headers.Add("API-Version", "2023-10");
request.Headers.Add("Authorization", "APITOKEN");
var content = new StringContent("{\"query\":\"{\\n  items_page_by_column_values(\\n    board_id: 1234567890\\n    columns: [{column_id: \\\"name\\\", column_values: \\\"DDNL05\\\"}]\\n  ) {\\n    cursor\\n    items {\\n      id\\n    }\\n  }\\n}\",\"variables\":{}}", null, "application/json");
request.Content = content;
var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
Console.WriteLine(await response.Content.ReadAsStringAsync());

Hope that helps!

Cheers,
Matias

1 Like

Hey guys,

Thank you both for the great suggestions, I have definitely added Postman to my tools. I wanted to give an update. I was able to get a working query this morning using JsonSerializer. with a basic HttpClient.

            string json =
                System.Text.Json.JsonSerializer.Serialize(
                    new
                    {
                        query = @"{
                              items_page_by_column_values (board_id: $board columns: [{column_id: ""name"", column_values: ""Test_01""}]) {cursor items{id}} }",
                    }
                );
1 Like

That’s great @J_Morl !!

1 Like