3

I use REST API in JavaScript. When I request REST API multiple times, it returns(response) invalid session id, but I am providing a valid session id, because I have pulled data with this session id.
Anyone came across this issue?

function sugarLogin(url, user, password) {
var request = new XMLHttpRequest();
var params = {
    user_auth: {
        user_name: user,
        password: password
    },
    name_value_list: [{
        name: 'notifyonsave',
        value: 'true'
    }]
};
var json = $.toJSON(params);
var crm_api = url;
request.open("POST", crm_api, true);
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
request.onreadystatechange = function () {
    if (request.readyState == 4 && request.status == 200) {
        var response = request.responseText;
        var response_obj = jQuery.parseJSON(response);

        if (response_obj) {
            if (response_obj.name && response_obj.name == "Invalid Login") {
                //invalid login
                ProcessingFlag = 3;
            } else {
                session_id = response_obj.id;
                ProcessingFlag = 1;
            }
        }
    } else if (request.readyState == 4 && request.status == 404) {
        ProcessingFlag = 2;
    }
}
request.send("method=login&input_type=JSON&response_type=JSON&rest_data=" + json);}

I have used above code to login and set session_id (global scope) and then using this session id I am calling search_by_module function of REST API. It is working fine but if made multiple requests frequently then it says invalid session id.

Although I have tried to call login function again before making search_by_module call. Main issue is when I tried calling other REST function after response returned from search_by_module and rendered HTML, it says me invalid session. I can't figure out why session expires immediately while we know that on server session expires after 24 minutes (server where our sugar instance hosted)

Be Brave Be Like Ukraine
  • 7,596
  • 3
  • 42
  • 66
Jaffar Hussain
  • 308
  • 2
  • 9
  • i have updated my question and added code for login and i using search_by_module same as this but rest_data and method to call is different. – Jaffar Hussain Nov 30 '12 at 11:33

1 Answers1

2

I bet you're getting this because of item number 3 :

1/ Defined an error callback that checks for that particular response -invalid login- and calls the login function. Reading your code, I guess this is ProcessingFlag = 3; job.

2/ make sure the login function updates the session_id correctly and globally so that future function calls will get the correct value.

3/ make sure you're passing that new session_id to all your subsequent calls as FIRST parameter in all you rest_data. I got caught on this and lost many hours only to find out that SugarCRM DOESN'T support named parameters in its rest_data (is it poorely implemented function-arguments-wise ?) This is because I was adding session_id as last parameter and all of my REST calls were returning invalid ID.

ychaouche
  • 4,922
  • 2
  • 44
  • 52
  • 2
    Already i am doing all as you said, but i found my problem, it is due to change in ip(dynamic ip changes frequently ). and sugar expire session due to significant change in ip. thanks all for your response. – Jaffar Hussain Dec 03 '12 at 06:21