Rest call to same instance from service portal widget

Vishnu V Reddy
Tera Contributor

Hi,

I have a custom widget to upload documents by selecting the table name and task number. This is done using REST POST method as shown below. This works fine as a logged in user. I am trying to achieve the same but as a different user. I did try using "'Authorization':'Basic '+btoa('username'+':'+'password')" in the header section of the below client controller script in widget, but nothing is happeing when I do this. Any ideas on how to do it?

$scope.uploadFiles = function() {
$scope.fd = new FormData();
$scope.files.forEach(function(file){
$scope.fd.set('files', file);
var request = {
method: 'POST',
url: 'https://instancename.service-now.com/api/now/attachment/file?table_name=incident&table_sys_id='+c.data.rec_sysid+'&file_name='+file.name,
data: $scope.fd.get('files'),
headers: {
'Content-Type': file.type,
'Accept':'application/json',
}
};
console.log('HTTP request:',request);

// SEND THE FILES.
$http(request)
.success(function (d) {
// On success code here
})
.error(function (err) {
// On error code here
});

});
}

 

2 REPLIES 2

Ashutosh Munot1
Kilo Patron
Kilo Patron

HI,

Can you try this:

headers: {
'Content-Type': file.type,
'Accept':'application/json',

'Authorization': 'Basic user:password'
}

 

Thanks,
Ashutosh

Hi, actually it worked with "'Authorization':'Basic '+btoa('username'+':'+'password')". I was using an incorrect username.

However, instead of just adding the attachment to the incident as this account, it is actually logging me in as the user account. Do you know if that can be prevented?