cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
426
Views
0
Helpful
4
Replies
Highlighted
Beginner

How to implement Basic Credentials when making REST webservice call

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
Highlighted

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
Highlighted
Cisco Employee

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

Highlighted

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

Highlighted

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

Highlighted

Hi Nick,

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

Best Regards,

Yudhi