I am learning the infobip API with documentation here I have the following working code in jQuery:
function testSMS(){
var settings = {
"url": "https://p932l.api.infobip.com/sms/2/text/advanced",
"method": "POST",
"timeout": 0,
"headers": {
"Authorization": "App API-KEY",
"Content-Type": "application/json",
"Accept": "application/json"
},
"data": JSON.stringify({
"messages": [
{
"destinations": [
{
"to": "012345678901"
}
],
"from": "InfoSMS",
"text": "This is a sample message"
}
]
}),
};
console.log(settings)
$.ajax(settings).done(function (response) {
console.log(response);
});
I am trying to convert it to work in Google Apps Script using UrlFetchApp().
function testInfoBip(){
var settings = {
"url": "https://p932l.api.infobip.com/sms/2/text/advanced",
"method": "POST",
"timeout": 0,
"headers": {
"Authorization": "App API-KEY",
"Content-Type": "application/json",
"Accept": "application/json"
},
"data":JSON.stringify({
"messages": [
{
"destinations": [
{
"to": "012345678901"
}
],
"from": "InfoSMS",
"text": "This is a sample message"
}
]
}),
};
var result = UrlFetchApp.fetch('https://p932l.api.infobip.com/sms/2/text/advanced', settings)
console.log(result);
}
The code is giving me
Exception: Request failed for https://p932l.api.infobip.com returned code 400. Truncated server response: {"requestError":{"serviceException":{"messageId":"BAD_REQUEST","text":"Invalid request body."}}}
I strongly suspect something is wrong with JSON.stringify
and its output. How can I troubleshoot this or correct my code?