Using Axios to post a message in Microsoft Teams

Solution 1:

You should let axios handle the calculation of the payload length, and therefore remove this line:

'content-length': `${card.toString().length}`

Your resulting script from your post would look like:

const axios = require('axios'); // axios must be installed via npm i axios
const webhookURL = "https://outlook.office.com/webhook/xxxxx"; // this holds my webhook URL

postMessageToTeams();

async function postMessageToTeams(title, message) {
    const card = {
      "@type": "MessageCard",
      "@context": "http://schema.org/extensions",
      "themeColor": "0072C6",
      "summary": "Summary description",
      "sections": [
        {
          "activityTitle": "Testcafe",
          "text": "Testcafe is talking to you"
        }
      ]
    };

    try {
      const response = await axios.post(webhookURL, card, {
        headers: {
          'content-type': 'application/vnd.microsoft.teams.card.o365connector'
        },
      });
      return `${response.status} - ${response.statusText}`;
    } catch (err) {
        console.log(err)
      return err;
    }
  }

Found this working for me.