Getting error 500 in json while creating button to startjobs

Hi guys,
I am trying to create a Button using html to startjobs. I have crated below script

getting an error below error
image

please correct me where I am doing wrong.

Thanks.

@loginerror
@HareeshMR @Palaniyappan @AshwinS2 @Pablito

Hi guys,
there is no comment from last 2Hrs.

please help me out.

Sorry for the delayed response
are we trying to connect the orchestrator with api
if so this 500 ERROR usually occurs when the Tenantname or username or password is set wrongly
kindly check that once
or
are we getting this error at some other integration

Cheers @Assetsoft

HI @Palaniyappan
thank you for replay.

same thing is working from postman. but not from html script i wrote.

may be I did something to pass json in html ? I am not sure ? please check script screen shot

please let me know if you need anything?

is the robot id correctly mentioned
image

kindly check once with ReleaseKey as well
because this error is basically dependent on the login details…so i hope we might be lagging there with improper details

@Assetsoft

Hi @Palaniyappan

Robot id and release key is correct. I think I might be wrong in passing headers may be wrong.
the values are correct but syntax I am not sure.

could you please check all syntax i wrote correctly. i have gone more time in that for me it looks good:sweat_smile::joy:

@loginerror
@HareeshMR @Palaniyappan @AshwinS2 @Pablito

good morning guys…
I am waiting for help. please help me out.

Hi,

Please add missing information like url, token, etc…, for below code and share the error console.

$.ajax({
    url: 'https://platform.uipath.com/',
    type: 'POST',
    beforeSend: function(xhr) {
        xhr.setRequestHeader('Authorization', '');
        xhr.setRequestHeader('X-UIPATH-TenentName', '');
        xhr.setRequestHeader('Content-Type', 'appliation/json');
    },
    data: {
        "startInfo": {
            "ReleaseKey": "5cbbdb95-3dc5-46cc-a442-9d89e3c09b70",
            "Strategy": "Specific",
            "RobotId": [224014],
            "NoOfRobots": 0
        }
    },
    dataType: 'json',
    success: function(result, status, xhr) {
        if (status === 200) {
            alert('Process will start!');
        } else {
            alert(status);
        }
    },
    error: function(xhr, status, error) {
        console.log(xhr);
        console.log(status);
        console.error(error);
    }
});

Hello @pkbharath
Thank you for replay. I tried as u given

Getting authentication error below

But when i am trying to run same from postman it is working. how ? why ?

Thanks.

As the error message states, you need to sign in to access the end point.

From where are you copying the bearer token?

@pkbharath
I perform authentication done from postman. and i am using all tokens, values from POSTMAN.

Please assist me if i am doing anything wrong.

There are couple of things:

  1. Can’t copy bearer token from postman to jquery code.
  2. Check if the server allows cross-domain request.
  3. Get the token before sending post request.

At postman you would generally give username & password to an end-point for getting the token.
Do the same call using ajax to get the token, then perform the post request using the new token.

@pkbharath
Thanks.

so as u said the access token I am using from POSTMAN is not going to work from ajax ??

As I am not aware of json & ajax. and i have requirement of start job from on click i.e button click.

Can you please give me link or document to get an access token pass into the ajax script which u shared.

I tried to find out CSRF and XSRF but i don’t get anything working.

@ Assetsoft

Some times just need to google it :slight_smile:

thanks @Luis261980
:joy:

the error reason is my syntax is wrong that I know,

the problem is I don’t know about JSON and AJAX. so need help sort it out.

500 is old one.
now I am getting 401 which regarding to authentication. but i did authentication from POSTMAN and access key’s I am using now.

do U think u can help me out ??

Hi, show how you get token at postman, like post call details. Then I could help you with JavaScript part of it.

Thanks @pkbharath
so to GET token from POSTMAN.
I am going through following steps. you can see there are multiple tabs opened step by step.
< Image removed by Forum Staff // please don’t share your API tokens publically >

let me know anything else u want to know.

Hi,

Please add bot start url, code_verifier, client_id & tenent_name for the below mentioned code and let me know if any errors occur.

Button function is calling getToken function with callback for getting the values and then we are asking bot to start.

Note: Getting token function needs to be bit more robust to check if the token has expired and then make the ajax request. For now, when ever the button clicks it will get the token. This part could be sorted once the basic dev is done.

let getToken = function(cb) {
    $.ajax({
        url: 'https://account.uipath.com/oauth/token',
        type: 'POST',
        data: {
            "grant_type": "authorization_code",
            "code": "3Q6y4rxLfK0XmA03",
            "redirect_uri": "https://account.uipath.com/mobile",
            "code_verifier": "",
            "client_id": ""
        },
        dataType: 'json',
        success: function(result, status, xhr) {
            if (status === 200) {
                console.log('Got token!');
                cb(null, result);
            } else {
                cb(`Could not get token we have status of ${status}`);
            }
        },
        error: function(xhr, status, error) {
            console.log(xhr);
            console.log(status);
            // console.error(error);
            cb(error);
        }
    });
};

let myFunction = function() {
    let tenent_name = '';
    getToken(function(error, result) {
        if (!error) {
            // console.log(result);  // Uncomment this like to check if the token details are present
            $.ajax({
                url: 'https://platform.uipath.com/',
                type: 'POST',
                beforeSend: function(xhr) {
                    xhr.setRequestHeader('Authorization', `${result.token_type} ${result.access_token}`);
                    xhr.setRequestHeader('X-UIPATH-TenentName', `${tenent_name}`);
                    xhr.setRequestHeader('Content-Type', 'appliation/json');
                },
                data: {
                    "startInfo": {
                        "ReleaseKey": "5cbbdb95-3dc5-46cc-a442-9d89e3c09b70",
                        "Strategy": "Specific",
                        "RobotId": [224014],
                        "NoOfRobots": 0
                    }
                },
                dataType: 'json',
                success: function(result, status, xhr) {
                    if (status === 200) {
                        console.log(result);
                        alert('Process will start!');
                    } else {
                        cb(`Could not start bot we have status of ${status}`);
                    }
                },
                error: function(xhr, status, error) {
                    console.log(xhr);
                    console.log(status);
                    console.error(error);
                    alert('Something went wrong!');
                }
            });
        } else {
            console.log(error);
            alert('Something went wrong!');
        }
    });
}

I will check and let you know…

Hi @Assetsoft

Did it work?