Riyafa Abdul Hameed
Riyafa Abdul Hameed

Reputation: 7973

GAS: Error when using UrlFetchApp

I am using the senotp serverside api to verify a number. When it is run using soap ui with the same content there's no error. But when I do it with UrlFetchApp it gives the error:

{"status":"error","response":{"code":"INVALID_REQUEST_BODY"}}

whereas the correct response should be:

{
   "status": "success",
   "response":    {
      "code": "OTP_SENT_SUCCESSFULLY",
      "oneTimePassword": "34859"
   }
}

The code I used is as foll0ws:

var payload =
      {
        "countryCode": "94",
        "mobileNumber": "0766075555",
        "getGeneratedOTP": true
      };

  var header=
      {
        "application-Key":"application_key_value"
      };


  var options =
   {
     "method" : "POST",
     "headers":header,
     "muteHttpExceptions": true,
     "contentType":"application/json",
     "payload" :  payload
   };

  var request=UrlFetchApp.getRequest("https://sendotp.msg91.com/api/generateOTP", options)
  Logger.log(request)
  var response=UrlFetchApp.fetch("https://sendotp.msg91.com/api/generateOTP", options);
  Logger.log(response);

I am not sure what is the problem here. Please help me debug it or let me know what is the problem with the code.

Upvotes: 0

Views: 343

Answers (1)

user3722096
user3722096

Reputation: 74

It seems as if using payload in GAS fetchService automatically results in contentType : application/x-www-form-urlencoded or contentType : multipart/form-data.

See documentention: https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app#fetch(String,Object)

Unfortunately, I have not yet found a workaround.

Upvotes: 1

Related Questions