Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SlackApi - Missing Linebreaks

Tags:

c#

.net

slack-api

I'm sending data to a slack channel with webhook integration. But when i include a linebreak (\n) i get at http 500. When I url encode the linebreak i see the encoded value, not a real line break in slacn

            string body = "foo"
            if(!string.IsNullOrWhiteSpace(feedEntry.Link ))
            {
                body +=  " \n <" + feedEntry.Link +">";
            }           
            body = "{\"text\": \"" + body+ "\"}";

            using(var client = new HttpClient())
            {
                var msg = new StringContent(body);
                var result = await client.PostAsync(url, msg);
            }
like image 816
Boas Enkler Avatar asked Dec 08 '25 09:12

Boas Enkler


1 Answers

Use \\n instead of \n. The string will then contain \ followed by n which is valid JSON and will turn into a newline char by the JSON parser.

like image 162
Robert Levy Avatar answered Dec 10 '25 07:12

Robert Levy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!