Created
June 6, 2021 05:39
-
-
Save ktskumar/96aba275482a6653c8c30c55a021e325 to your computer and use it in GitHub Desktop.
HTTP Post request to be used with SharePoint Online REST API to do the post activities
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// POST request Call | |
function RestRequest(url, params) { | |
var req = new XMLHttpRequest(); | |
return new Promise(function(resolve, reject) { | |
req.onreadystatechange = function() { | |
if (req.readyState != 4) // Loaded | |
return; | |
if (req.status >= 200 && req.status < 300) { | |
resolve(req); | |
} else { | |
reject({ | |
status: req.status, | |
statusText: req.statusText | |
}); | |
} | |
}; | |
// Prepend web URL to url and remove duplicated slashes. | |
var webBasedUrl = (_spPageContextInfo.webServerRelativeUrl + "//" + url).replace(/\/{2,}/, "/"); | |
req.open("POST", webBasedUrl, true); | |
req.setRequestHeader("Content-Type", "application/json;odata=verbose"); | |
req.setRequestHeader("ACCEPT", "application/json;odata=verbose"); | |
req.setRequestHeader("x-requestdigest", _spPageContextInfo.formDigestValue); | |
req.send(params ? JSON.stringify(params) : void 0); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment