Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

meteor http post to other domain

So I want to use an sms service from 46elks in my meteor project. The following php script allows you to send an sms:

<?
// Example to send SMS using the 46elks service
// Change $username, $password and the mobile number to send to

function sendSMS ($sms) {

  // Set your 46elks API username and API password here
  // You can find them at https://dashboard.46elks.com/
  $username = 'u2c11ef65b429a8e16ccb1f960d02c734';
  $password = 'C0ACCEEC0FAFE879189DD5D57F6EC348';

  $context = stream_context_create(array(
    'http' => array(
      'method' => 'POST',
      'header'  => "Authorization: Basic ".
                   base64_encode($username.':'.$password). "\r\n".
                   "Content-type: application/x-www-form-urlencoded\r\n",
      'content' => http_build_query($sms),
      'timeout' => 10
  )));

  return false !== file_get_contents(
    'https://api.46elks.com/a1/SMS', false, $context );
}


$sms = array(
  'from' => 'DummyFrom',   /* Can be up to 11 alphanumeric characters */
  'to' => '+46400000000',  /* The mobile number you want to send to */
  'message' => 'Hello hello!'
);
sendSMS ($sms);

?>

Now I need this in my meteor project and I've been trying to convert it to meteors http.call():

HTTP.call("POST", "https://api.46elks.com/a1/SMS", {
                headers:
                {
                    "Authorization": "Basic SomeLongBase46EncodedString",
                    "Content-type": "application/x-www-form-urlencoded"
                },

                data:
                {
                    "from": "testFrom",
                    "to": "+46701111111",
                    "message": "test message"
                }
            },

            function (error, result)
            {
                if (error)
                {
                    console.log("error: " + error);
                }
                else
                {
                    console.log("result: " + result);
                }
            });

But what I keep getting is the following error:

error: Error: failed [403] Missing key from

like image 443
jt123 Avatar asked Mar 19 '23 19:03

jt123


1 Answers

Change data to params:

params: {
      "from": "testFrom",
      "to": "+46701111111",
      "message": "test message"
}

and use auth instead of Authorization (docs):

"auth": username + ":" + password
like image 137
Tarang Avatar answered Mar 21 '23 09:03

Tarang