HTTP Request to access the Microtronics REST API

Hello community

I followed the Topic Accessing Microtronics REST API with curl . I used GIT Bash to send the request and it worked perfectly. Now I tried to access the Microtronics REST API without cURL but using only the protocol https with Javascript.

When I make a request I only get the json with err: “E_UNAUTHORIZED” back.

I am using the URL:
https://USERNAME:PASSWORD@cloud.microtronics.com/api/1/me

How do I have to include my username and password in the URL?

Greetings
Jakob

Hello community

For the problem I found by myself a solution.

Gretting
Jakob

Hello Jakob,

in order to provide the authentication information it is necessary to set the following http header:

“Authorization”: "Basic " + base64_encode(username:password)

For using jquery ajax this could be done like:

$.ajax({
  ...
  beforeSend: function (xhr) {   // add authorization header before sending the request
    xhr.setRequestHeader ("Authorization", "Basic " + base64_encode(username + ":" + password));
   },
  ...
});

Using axios (a widespread library to access REST API’s):

axios.get(url, {}, {
  headers: { "Authorization":  "Basic " + base64_encode(username:password)  }
}).then(function(response) {
  console.log('Authenticated');
}).catch(function(error) {
  console.log('Error on Authentication');
});

Greetings, Manuel