-1

I made a template using Slack block kit builder

I read some data and fill eight parameters

in my C# code I have to use interpolated string and curly braces to supply the parameters {}. I also need to skip the braces by adding them twice.

but this is not recognized as correct json when I make the post message call :

var data = new NameValueCollection();
data["token"] = token;
data["channel"] = channelName;
data["as_user"] = "true";           
data["text"] = postedMessage;
data["blocks"]=jsonstring; 
var client = new WebClient();
var response = client.UploadValues("https://slack.co/api/chat.postMessage", "POST", data);
string responseInString = Encoding.UTF8.GetString(response);
Console.WriteLine(responseInString);

I understand the correct way is to represent these blocks as classes . I tried to follow the slack attachment representation but the blocks are more complex containing inner objects and not string variables as the attachment class . Appreciate your support to represent the blocks using easy syntax and correct json

Coder
  • 121
  • 9
  • Okay , I will go over the example. But will I be able to include variables within my json string to fill the parameters? – Coder Jun 24 '19 at 20:14
  • Yes, that is possible. But it can be tricky, because you will need to escape some characters the JSON way for this to work, e.g. special characters in UTF-8 – Erik Kalkoken Jun 24 '19 at 20:17
  • see this for the official documentation on how that works for JSON: https://www.json.org/ – Erik Kalkoken Jun 24 '19 at 20:19

1 Answers1

2

The reason your code does not work is that in C# single quotes only work for single characters. Here is how to properly escape double quotes and curly brackets of a JSON for hard-coded string constants.

normal string constants

To properly escape the hard-coded JSON inside your code you need to add a backslash to each and every double quotes of your JSON string.

Example:

string someJson = "{\"ErrorMessage\": \"\",\"ErrorDetails\": {\"ErrorID\": 111,\"Description\":{\"Short\": 0,\"Verbose\": 20},\"ErrorDate\": \"\"}}";

There are other ways on how to do it. See this question for a full overview of the solutions.

Btw. you will not need to escape curly brackets in this approach.

interpolated string constants

With interpolated string you also need to escape the double quotes. But here it works by doubling them.

Example:

string someJson = "{\"ErrorMessage\": \"\",\"ErrorDetails\": {\"ErrorID\": 111,\"Description\":{\"Short\": 0,\"Verbose\": 20},\"ErrorDate\": \"\"}}";

For interpolated strings you will also need to escape the curly brackets. This works by doubling the escaped curly brackets, since the single curly brackets are used for interpolating.

Example:

var json = $@"{{""name"":""{name}""}}";

See also this answer for a full discussion on how to escape curly brackets.

Erik Kalkoken
  • 30,467
  • 8
  • 79
  • 114
  • I will add the backslash to every double qoute but how do I manager the interpolation braces? String someJson =$ “" \attribute:\“{variable1} " – Coder Jun 24 '19 at 20:25
  • but if you must, you can escape the curley brackets too within an interpolated string. Let me add to the answer. – Erik Kalkoken Jun 24 '19 at 20:32
  • 1
    It worked. You have no idea how much I appreciate your support. Maybe I need to re-write the question to better address the issue. thank you so much really – Coder Jun 24 '19 at 21:01