AnsweredAssumed Answered

POST multiple projects via. REST API

Question asked by DJ.V on May 26, 2017
Latest reply on Jun 7, 2017 by DJ.V

So, I'm trying to create a sample script using JS.

 

It fails with bad request 400. I think it has to do with how I'm call SEND() request.

xhr.send(JSON.stringify(myProjects));

 

If I use REST API documentation url and try to execution action for multiple project it works. 

 

Now I do not know if I need to send only 1 project request at a time using LOOP or it would allow me pass full-array and API will handle rest. Any one has tried posting multiple projects using JS?

 

Here is full-script:

<script>
var cappm_apiURL = "http://clarity123.com/ppm/rest/v1";
var username = "username@clarity123.com"; // Update username
var password = "password*"; // Update password
var URL = cappm_apiURL + "/projects";
var myheader = btoa(username + ":" + password);

var myProjects = {"_results": [{"code":"ABC1000009","name":"This is My Project Name","scheduleStart": "2017-01-01T08:00:00","scheduleFinish": "2017-12-31T17:00:00","description": "This is Project Description"},{"code":"ABC1000007","name":"This is My Project Name","scheduleStart": "2017-01-01T08:00:00","scheduleFinish": "2017-12-31T17:00:00","description": "This is Project Description"}]};

var xhr = new XMLHttpRequest();
xhr.open("POST", URL, true);

//Send the proper header information along with the request
xhr.setRequestHeader ("Authorization", "Basic " + myheader);
xhr.setRequestHeader ("Content-type", "application/json");

xhr.onreadystatechange = function() {//Call a function when the state changes.
if(xhr.readyState == 4 && xhr.status == 200) {
alert("Projects are being created...");
}
}
alert("All Records:" + JSON.stringify(myProjects));
xhr.send(JSON.stringify(myProjects));
</script>

Outcomes