How to create a site with REST API from javascript?

cancel
Showing results for 
Search instead for 
Did you mean: 
anandhan
Active Member II

How to create a site with REST API from javascript?

This is what am trying to do ,top create a site from REST API in javascript.

(Alfresco community edition 201707)

function CreateSite(){
/*TEST API - START*/
var siteBodyCreate = {
"id": "NewTest",
"title": "NewTest",
"description": "NewTest",
"visibility": "PUBLIC"
};

$.ajax({
type: 'POST',
beforeSend: function(request) {
request.setRequestHeader("Authority", 'Basic YWRtaW46YWRtaW4=');
},
headers: {
'Content-Type': 'application/json',
'Accept':'application/json',
'Authorization': 'Basic YWRtaW46YWRtaW4='
},
dataType: 'json',
url: 'http://127.0.0.1:8080/alfresco/api/-default-/public/alfresco/versions/1/sites?skipConfiguration=fals...',
data:JSON.stringify({"entry" : siteBodyCreate}) ,
success: function(data) {
alert(data);
} ,
failure : function(data){
alert(data);
}
});
/*TEST API - END*/
}

i get the following error

{
"error": {
"errorKey": "Could not read content from HTTP request body: Unrecognized field \"entry\" (Class org.alfresco.rest.api.model.Site), not marked as ignorable\n at [Source: java.io.BufferedReader@5ad545e1; line: 1, column: 11] (through reference chain: org.alfresco.rest.api.model.Site[\"entry\"])",
"statusCode": 400,
"briefSummary": "07300002 Could not read content from HTTP request body: Unrecognized field \"entry\" (Class org.alfresco.rest.api.model.Site), not marked as ignorable\n at [Source: java.io.BufferedReader@5ad545e1; line: 1, column: 11] (through reference chain: org.alfresco.rest.api.model.Site[\"entry\"])",
"stackTrace": "For security reasons the stack trace is no longer displayed, but the property is kept for previous versions",
"descriptionURL": "https://api-explorer.alfresco.com"
}
}

What is the right approach?

2 Replies
janv
Alfresco Employee

Re: How to create a site with REST API from javascript?

Hi,

Please check your JSON payload. You do not need the top-level "entry" ...

You may find it useful to sanity check via "curl" or Postman (or your favourite HTTP/REST client) ? You may also find it useful to take a look at https://community.alfresco.com/community/ecm/blog/2017/02/24/v1-rest-api-part-8-sites or simply "Try it out !" via the API explorer to see an example payload Alfresco Content Services REST API Explorer ?

Another option is to have a look at how it is done via the Alfresco JS API "create site" or use that instead ?

Hope that helps.

Regards,

Jan

anandhan
Active Member II

Re: How to create a site with REST API from javascript?

Hi Jan Vonkan,

It was helpul.

But now i have end up with error saying "Can not get preferences for admin because there was an error pasing the JSON data"