0

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?

Barth
  • 11
  • 5
  • Please modify `"data":JSON.stringify({` to `"payload":JSON.stringify({` and test it again. [Ref](https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app#fetchurl,-params) By the way, there is no property of `timeout` in UrlFetchApp. – Tanaike Oct 31 '22 at 11:04
  • This worked. I can't believe it was this simple. Thank you for saving the day. – Barth Oct 31 '22 at 11:16
  • Thank you for replying. I'm glad your issue was resolved. In this case, I flagged this as a duplicated question. – Tanaike Oct 31 '22 at 11:18

0 Answers0