Get RESTApi response

cancel
Showing results for 
Search instead for 
Did you mean: 
jackarnd
Established Member

Get RESTApi response


Hello,
I'd like to parse a string given by : http://localhost:8080/alfresco/api/-default-/public/alfresco/versions/1/people but my code doesn't log in alfresco.
Thus, I wrote this code in my custom share page:

<p id="peoplelist"></p>

<script>
   
var people = new XMLHttpRequest();
   
people.onreadystatechange = function() {
      
if (this.readyState == 4 && this.status == 200) {
         
obj = JSON.parse(this.responseText);
         
document.getElementById("peoplelist").innerHTML = obj.list.entries[0].entry.firstName + obj.list.entries[0].entry.lastName + "," + obj.list.entries[0].entry.email;
      
}
   
};
   
people.open('GET', 'http://localhost:8080/alfresco/api/-default-/public/alfresco/versions/1/people'true, 'admin', 'admin');
   
people.send();
</script>

So, this gives me a basic 401 console error about the authentification

:8080/alfresco/api/-default-/public/alfresco/versions/1/people Failed to load resource: the server responded with a status of 401 (Unauthorized)
new-page:1 XMLHttpRequest cannot load http://localhost:8080/alfresco/api/-default-/public/alfresco/versions/1/people. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8081' is therefore not allowed access. The response had HTTP status code 401.

Besides, when I go on http://localhost:8080/alfresco/api/-default-/public/alfresco/versions/1/people  the basic login pop-up opens in which I can log in with admin/admin and get the expected result:

{"list":{"pagination":{"count":7,"hasMoreItems":false,"totalItems":7,"skipCount":0,"maxItems":100},"entries":[{"entry":{"lastName":"Beecher","userStatus":"Helping to design the look and feel of the new web site","jobTitle":"Graphic Designer","statusUpdatedAt":"2011-02-15T20:20:13.432+0000","mobile":"0112211001100","emailNotificationsEnabled":true,"description":"Alice is a demo user for the sample Alfresco Team site.","telephone":"0112211001100","enabled":false,"firstName":"Alice","skypeId":"abeecher","avatarId":"198500fc-1e99-4f5f-8926-248cea433366","location":"Tilbury, UK","company":{"organization":"Moresby, Garland and Wedge","address1":"200 Butterwick Street","address2":"Tilbury","address3":"UK","postcode":"ALF1 SAM1"},"id":"abeecher","email":"abeecher@example.com"}},{"entry":{"firstName":"Administrator","emailNotificationsEnabled":true,"company":{},"id":"admin","enabled":true,"email":"admin@alfresco.com"}},{"entry":{"firstName":"Alex","lastName":"lol","emailNotificationsEnabled":true,"company":{},"id":"alexandra","enabled":true,"email":"alexandra.lol@test.com"}},{"entry":{"firstName":"Guest","emailNotificationsEnabled":true,"company":{},"id":"guest","enabled":false}},{"entry":{"firstName":"Jack","lastName":"lol","emailNotificationsEnabled":true,"company":{},"id":"jack","enabled":true,"email":"jack.lol@test.com"}},{"entry":{"lastName":"Jackson","userStatus":"Working on a new web design for the corporate site","jobTitle":"Web Site Manager","statusUpdatedAt":"2011-02-15T20:13:09.649+0000","mobile":"012211331100","emailNotificationsEnabled":true,"description":"Mike is a demo user for the sample Alfresco Team site.","telephone":"012211331100","enabled":false,"firstName":"Mike","skypeId":"mjackson","avatarId":"3fbde500-298b-4e80-ae50-e65a5cbc2c4d","location":"Threepwood, UK","company":{"organization":"Green Energy","address1":"100 Cavendish Street","address2":"Threepwood","address3":"UK","postcode":"ALF1 SAM1"},"id":"mjackson","email":"mjackson@example.com"}},{"entry":{"firstName":"Nicolas","lastName":"lol","emailNotificationsEnabled":true,"company":{},"id":"nicolas","enabled":true,"email":"nicolas.lol@test.com"}}]}}

If anyone knows where is my mistake thanks in advance !

7 Replies
mehe
Senior Member II

Re: Get RESTApi response

Hi Jack,

I'm bit puzzled by the message about port 8081 but you could try to enable cors in the web.xm of alfresco like described in the configuration for the alfresco ng-components: alfresco-ng2-components/ALFRESCOCORS.md at master · Alfresco/alfresco-ng2-components · GitHub 

Maybe this helps...

cu Martin

jackarnd
Established Member

Re: Get RESTApi response

Hi !
The port 8081 is because I used Maven to create my Alfresco repository and Share too. So Alfresco is running on 8080 and Share on 8081. So I've looked into enabling CORS in an Alfresco repo generated by Maven previously but it doesn't really work I think, the web.xml aren't the same, when I add the lines missing to enable CORS it bugs the compilation and so on...
But since Share works properly during the normal use I don't think that the CORS are the problem ? I don't know.. 

mehe
Senior Member II

Re: Get RESTApi response

...but you are starting the XMLHttprequest from the html page - that means from your browser - if I understood the problem.

There is also the enablecors platform module JAR (see the above link) - maybe you can include this to your repo...

jackarnd
Established Member

Re: Get RESTApi response

Ok I've seen with someone how to enable CORS in project. So I added these lines into my Alfresco repo (and put the lines in the web.xml such as said in the github link, and added the jar wherever I could or seemed right aha) :

<!-- Enabling CORS -->
<dependency>
      <groupId>org.alfresco</groupId>
      <artifactId>enablecors</artifactId>
      <version>1.0</version>
</dependency>
<dependency>
      <groupId>com.thetransactioncompany</groupId>
      <artifactId>cors-filter</artifactId>
      <version>2.5</version>
</dependency>
<!-- End enabling CORS -->

But I still get the same problems..

I was also wondering since I'm creating a new page in Share do I have to add lines into the configurations xml files of this page to access the Alfresco database ? My xml files only contain :

The first one:

<page>
<template-instance>new-page</template-instance>
<authentication>none</authentication> <!-- J'ai essayé avec une auth user aussi -->
</page>


The second one:

<template-instance>
<template-type>new-page/new-page</template-type>
</template-instance>

kevinr1
Active Member II

Re: Get RESTApi response

The problem isn't related to CORS. You say "custom share page" - which means you should be calling all Alfresco APIs through the Share proxy to piggy-back onto the existing authentication that Share is already using. You are trying to call Alfresco server directly, which is a bad idea from Share - as you have no idea what authentication chain is being used etc.

To call that API through Share and get the result is simply a matter of changing the GET URL:

http://localhost:8081/share/proxy/alfresco-api/-default-/public/alfresco/versions/1/people 

That will return the result you want, it will also use the existing authentication for the logged in Share user.

Cheers,

Kevin

jackarnd
Established Member

Re: Get RESTApi response

Ok thank you a lot !! That works fine !

With gratitude,

Jack

jackarnd
Established Member

Re: Get RESTApi response

I was wondering, I have an Apache2 server running on a Raspberry Pi, so I wanted to know if the same page I'm working on could work. But there is no share proxy. so I used the code I made at the beginning to access the api 

people.open('GET', 'http://192.168.1.103:8080/alfresco/api/-default-/public/alfresco/versions/1/people', true, 'admin', 'admin');

But this doesn't work (as kinda expected).

So how am I supposed to access alfresco from another instance than Share ?

Keeping up,

Jack.