Home > Mobile >  How to Convert cURL POST Request in PHP to Google Apps Script
How to Convert cURL POST Request in PHP to Google Apps Script

Time:11-03

VoIP.ms offers the ability to send SMS text messages from its API. They provide the following sample code:

<?
$postfields = array(
    'api_username'=>'[email protected]',
    'api_password'=>'password',
    'method'=>'getServersInfo',
    'server_pop'=>'1'
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_POST, true );
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($ch, CURLOPT_URL, "https://voip.ms/api/v1/rest.php");
$result = curl_exec($ch);
curl_close($ch);

$data=json_decode($result,true);

echo "<pre>";
print_r($data);
echo "</pre>";
?>

When I execute the following command from the command line in Terminal, I'm able to successfully send an SMS text message from VoIP.ms:

curl -X POST -F '[email protected]' -F 'api_password=password' -F 'method=sendSMS' -F 'did=1234567890' -F 'dst=0987654321' -F 'message=Hello' https://voip.ms/api/v1/rest.php

By searching around on Google, I've cobbled together the following Google Apps Script:

function sendSMS() {
  var formData = {
    api_username : "[email protected]",
    api_password : "password",
    method : "sendSMS",
    did : 1234567890,
    dst : 0987654321,
    message : "Hello"
};
  var options = {
    method : "POST",
    payload : formData
};
  UrlFetchApp.fetch("https://voip.ms/api/v1/rest.php", options);
}

When I run the script, I get the following error:

Exception: Request failed for https://voip.ms returned code 500. Truncated server response: > xml version="1.0" encoding="UTF-8"?> <env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">env:Bodyenv:Faultenv:Codeenv:Value... (use muteHttpExceptions option to examine full response) sendSMS @ Code.gs:14

Any suggestions on where I'm going wrong?

CodePudding user response:

I believe your goal is as follows.

  • You want to convert the following curl command to Google Apps Script. And, your curl command works fine.

      curl -X POST -F '[email protected]' -F 'api_password=password' -F 'method=sendSMS' -F 'did=1234567890' -F 'dst=0987654321' -F 'message=Hello' https://voip.ms/api/v1/rest.php
    

Modification points:

  • From your sample curl command, it seems that did : 1234567890 and dst : 0987654321 should be did : "1234567890" and dst : "0987654321". This has already been mentioned in the TheMaster's comment.

  • In your curl command, -F is used. In this case, the content type of the request header is multipart/form-data. Unfortunately, UrlFetchApp uses application/x-www-form-urlencoded as the default content type.

When these points are reflected in tha Google Apps Script, how about the following modification?

Modified script:

function sendSMS() {
  var formData = {
    api_username: "[email protected]",
    api_password: "password",
    method: "sendSMS",
    did: "1234567890",
    dst: "0987654321",
    message: "Hello"
  };
  var options = { payload: Object.entries(formData).reduce((o, [k, v]) => (o[k] = Utilities.newBlob(v), o), {}) };
  UrlFetchApp.fetch("https://voip.ms/api/v1/rest.php", options);
}

Note:

  • This modified script supposes that your URL of https://voip.ms/api/v1/rest.php can be accessed from the Google side, and the values of your formData are valid values. Please be careful about this.

  • At UrlFetchApp, the request body of multipart/form-data is automatically created in the internal server side. But, if this request body couldn't be used to your API (There is sometimes a case that this request body cannot be used.), please test the request using FetchApp (Author: me).

Reference:

  • Related