cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
844
Views
0
Helpful
4
Replies

How to implement Basic Credentials when making REST webservice call

yudhitanaka
Level 1
Level 1

Hi,

I want to update one of the call variables in dialog object by making a REST webservice request (PUT), and embedded the Basic Credentials in the webservice request.

Can I get an example how to implement Basic Credentials when making REST webservice call?

Thanks in advance!

Best Regards,

Yudhi

1 Accepted Solution

Accepted Solutions

Hi Yudhi,

Basic authentication is simple with jQuery.  Just add this to your $.ajax request. 

headers: {
"Authorization": "Basic " + btoa(USERNAME + ":" + PASSWORD)
},

For example:

$.ajax({
type: 'PUT',
url: 'some-url',
headers: {
"Authorization": "Basic " + btoa(USERNAME + ":" + PASSWORD)
},
dataType: 'xml',
data: 'some-xml-data',
success: function (){
alert('Success!');
}
});

Keep in mind that if you're using jQuery (or any other AJAX method) to do a REST PUT operation, you have to take CORS into account.  Your $.ajax URL has to be on the same server as the web application you're writing.  Otherwise you'll get an error like this:  XMLHttpRequest cannot load [URL]. No 'Access-Control-Allow-Origin' header is present on the requested resource.

Unfortunately, there's no easy answer to that problem.

Thanks,

Nick

View solution in original post

4 Replies 4

npetrele
Cisco Employee
Cisco Employee

Are you talking about Basic Authentication (base64 encoded username:password)?  If so, what language are you using to perform the REST operation? 

Hi Nicholas,

Thanks for your response.

Yes, I'm talking about Basic Authentication (base64 encoded username:password). I'm using JavaScript for generate the REST request and perform the REST operation using jQuery (send Ajax request to backend).

Thanks,

Yudhi

Hi Yudhi,

Basic authentication is simple with jQuery.  Just add this to your $.ajax request. 

headers: {
"Authorization": "Basic " + btoa(USERNAME + ":" + PASSWORD)
},

For example:

$.ajax({
type: 'PUT',
url: 'some-url',
headers: {
"Authorization": "Basic " + btoa(USERNAME + ":" + PASSWORD)
},
dataType: 'xml',
data: 'some-xml-data',
success: function (){
alert('Success!');
}
});

Keep in mind that if you're using jQuery (or any other AJAX method) to do a REST PUT operation, you have to take CORS into account.  Your $.ajax URL has to be on the same server as the web application you're writing.  Otherwise you'll get an error like this:  XMLHttpRequest cannot load [URL]. No 'Access-Control-Allow-Origin' header is present on the requested resource.

Unfortunately, there's no easy answer to that problem.

Thanks,

Nick

Hi Nick,

Thank you for your help! It's really helpful!

Best Regards,

Yudhi

Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: