I am sending a Post request, with JavaScript Fetch method. The post attempts to create a Microsoft Teams chat message, via the Microsoft Graph Teams API. However, the server rejects the post request, with the message "missing body content". The payload shows the message body is being sent (Please see screenshot). It appears that the server is rejecting my Post request because it can not parse the body, which is in json format. Can anyone advise how I fix this Fetch issue? I've attached a code snippet for your review.
const data = {content: 'Hello Word'};
const options = {
method: 'POST',
headers: {
Authorization: 'Bearer '+response.accessToken,
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
};
fetch(endpoint, options)
.then((response)=>{
const data = response.json();
console.log("RESPONSE1:", data);
return data;
})
.then((data)=>{
console.log("RESPONSE2:", data);
callback(data, endpoint);
})
.catch((error) => console.log("PUSHMSGRAPH-Error:", error));
Try using the API and post it from the reference page with this API
POST /teams/{team-id}/channels/{channel-id}/messages
const teamId = '5834e4c1-2897-4cc3-b21900b56';
const threadid = '19:1df3a1d...@thread.tacv2';
const options = {
authProvider,
};
const client = Client.init(options);
const chatMessage = {
body: {
content: 'Hello World'
}
};
await client.api('/teams/' + teamId + '/channels/' + threadid + '/messages')
.post(chatMessage);