AnsweredAssumed Answered

Problem using REST to login in v6.5

Question asked by Mike Solomon on May 1, 2014
Latest reply on May 2, 2014 by Mike Solomon
I'm trying to use rest with sugar 6.5

I thought I'd start with a simple login

Unfortunately it is not working and I need some help to find what I am doing wrong

I tried both with php and with javascript and they both fail

<?php
// specify the REST web service to interact with
$url = "https://mydomain/service/v4_1/rest.php";

// Open a curl session for making the call
$curl = curl_init($url);
// Tell curl to use HTTP POST
curl_setopt($curl, CURLOPT_POST, true);
// Tell curl not to return headers, but do return the response
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// Set the POST arguments to pass to the Sugar server
$parameters = array(
    'user_name' => 'admin',
    'password' => 'password',
    );
$json = json_encode($parameters);
$postArgs = 'method=login&input_type=json&
      response_type=json&rest_data=' . $json;
curl_setopt($curl, CURLOPT_POSTFIELDS, $postArgs);
// Make the REST call, returning the result
$response = curl_exec($session);
// Close the connection
curl_close($session);
// Convert the result from JSON format to a PHP array
$result = json_decode($response);
// Echo out the session id
echo 'result' . $result['id'];
And my Javascript
var api_url = https://mydomain/service/v4_1/rest.php";
var user_name = 'admin';    //SugarCRM username
var password = 'password';    //SugarCRM password

var params = {
    user_auth:{
        user_name:user_name,
        password:password,
        encryption:'PLAIN'
    },
    application: 'SugarCRM RestAPI Example'
};
var json = JSON.stringify(params);

$.ajax({
        url: api_url,
        type: "POST",
        data: { method: "login", input_type: "JSON", response_type: "JSON", rest_data: json },
        dataType: "json",
        success: function(result) {
           
            alert('success')
           
             if(result.id) {
                    //HERE: you will have out put from rest
                alert("sucessfully LOGIN Your session ID is : " + result.id);
             }
             else
                 alert("Error noid");
             
        },
        error: function(result) {
           alert("Error - Failed");
        }
});

Any help in finding where I'm going wrong would me much appreciated



Outcomes