Error 403 occurs because of the failure of security validation in the page. You need to include Form-Digest Value in the REST API.
Refer the code below.
var _formDigest=getFormDigest();
var url = "http://<site>" + "/_api/lists/getByTitle('MyLib')/rootfolder/folders/add('MyFolder')";
$.ajax({
url: url,
type: "POST",
xhrFields: { withCredentials: true },
headers: { "accept": "application/json;odata=verbose", "x-requestdigest": _formDigest },
success: function (data) {
console.log("Done..");
},
error: function () {
console.log("Error Occured");
}
});
function getFormDigest() {
$.ajax({
url: "http://<site>" + "/_api/contextinfo",
type: "POST",
dataType: "json",
headers: { "Accept": "application/json; odata=verbose" },
xhrFields: { withCredentials: true },
async: false,
success: function (data) {
_formDigest = data.d.GetContextWebInformation.FormDigestValue;
console.log(data.d.GetContextWebInformation.FormDigestValue);
},
error: function (error) {
console.log(error.responseJSON.error.message.value);
}
});
return _formDigest;
}
The above code should work, but in your case since you are calling the api from JAVA code (I assume) you need to make certain changes in the SharePoint Server.
Please refer this LINK if you get other issues like CORS etc.