I would like to construct a dynamic json and do a post call. I am using ${data.list.name} to get the value from an object but it is not working. Is there a way to do this?
function callTeams (data) {
fetch(WEBHOOK, {
'method': 'POST',
'headers': { 'Content-Type': 'application/json' },
'body': JSON.stringify({
"@type": "MessageCard",
"@context": "http://schema.org/extensions",
"themeColor": "0076D7",
"sections": [{
"activityTitle": "${data.list.name} is valid",
"activityImage": "https://teamsnodesample.azurewebsites.net/static/img/image5.png",
"facts": [ {
"name": "Key",
"value": "${data.list.name}"
}],
"markdown": true
}],
"potentialAction": [ {
"@type": "OpenUri",
"name": "Submit",
"targets": [{
"os": "default",
"uri": "https://learn.microsoft.com/outlook/actionable-messages"
}]
}]
})
})
}
asked Oct 11, 2022 at 17:01
ezhil
1,1137 gold badges18 silver badges44 bronze badges
1 Answer 1
Use template literals. Replace things like
{
"activityTitle": "${data.list.name} is valid"
}
with
{
"activityTitle": `${data.list.name} is valid`
}
In other words, use backticks (`)
answered Oct 11, 2022 at 17:06
Yanick Rochon
53.8k30 gold badges147 silver badges233 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
ezhil
Thanks for your response. tried with both double quote and single quote but it is not working. I got response like $ is valid.
Yanick Rochon
It's neither double quote or single quote, it's the ` character
ezhil
ohh yeah. it worked with ` character. Thanks
lang-js
namewith"${...}"? Past value directly if it is string or use``