MS Teams Webhook returns Error 413 on small messages

122 views Asked by At

According to the MS documentation the maximum size of a message I can sent via webhook into a Teams channel is ~28KB (Link) But even on significantly smaller messages I receive the following error: Webhook message delivery failed with error: Microsoft Teams endpoint returned HTTP error 413 with ContextId ***

I played around a bit and figured out that I can only send request up to a content-size of 14.000 bytes.

Here is a simplification of my code:

private async Task SendTeamsNotification(string webhookUrl, string jsonTemplate) {
    var content = new StringContent(jsonformattedTemplate);
    var response = await _httpClient.PostAsync(webhookUrl, content)

    // Some error handling...
}

The jsonTemplate is a json-formatted string for an adaptive card. _httpClient is the default client.

What am I missing? Why is the limit exactly cut in half?

1

There are 1 answers

5
fzbm On

UPDATE: It looks like that the Incoming Webhook service converts the body to Unicode, when the request does not state that this is already the case by providing the CharSet alongside with the content type.

var content = new ByteArrayContent(encodedCard, offset: 0, count: bytesWritten)
{
    Headers =
    {
        ContentType = new MediaTypeHeaderValue("application/json")
        {
            CharSet = Encoding.Unicode.WebName
        }
    }
};

With that I was able to send an about 20 KB body (the card and the wrapper JSON containing the type and the "attachments") to the endpoint without any issues. More than 20 and technically about 28 KB should be possible, but in my case the dynamic generation logic for the card yielded me a 20 KB one.

This also explains why Robin was able to send a request with a maximum of 14 KB, because the service converted the body to Unicode and produced about 28 KB (each character requires at least two bytes).


I can not create a comment, but I think this has something to do with the encoding of the data when sending the card to Teams. It looks like Microsoft is checking the actual byte length instead of how many characters it consists of.

When not providing an encoding, StringContent uses UTF-8 to get the bytes. Providing for example four normal characters to this encoding yields a byte size of four, but providing the Japanese text ใƒ†ใ‚นใƒˆ (translated from test) yields a size of nine. Basically all non-normal characters consist of more than one byte (when processed by an encoding supporting them).

I'm also facing the task of sending dynamic content via an incoming webhook to a Teams channel and had the same question, because I already encountered a similar issue in another part of O365.

Groups are allowed to have a description with a maximum length of 1024, but it looks like Microsoft also counts the bytes instead of the characters, even if the description is part of a larger JSON body sent to Graph. A description of 1024 characters with at least one special one will cause an error. I assume the actual source of the problem is the backend service which Graph calls in order to set the description. But that is another story and not a part of the question of this thread.