RequestDigest as a key through which,
You can GET information without using it.
There is some information you cannot GET without using it.
You need the key to PUT (POST) information in SharePoint.
When executing non-GET REST requests to the SharePoint API, you must add a valid request digest to your request. This digest proves validity of your request to SharePoint. Because this token is valid only for a limited period of time, you have to ensure that the token you have is valid, before adding it to your request or the request will fail.
SharePoint includes a request digest token on the page in a hidden field named __REQUESTDIGEST.
One of the most common approaches to work with the request digest, is to obtain it from that field and add it to the request, for example
var digest = $('#__REQUESTDIGEST').val();
$.ajax({
url: '/_api/web/...'
method: "POST",
headers: {
"Accept": "application/json; odata=nometadata",
"X-RequestDigest": digest
},
success: function (data) {
},
error: function (data, errorCode, errorMessage) {
}
});
Such request would work initially, but if the user would have the page open for a longer period of time, the request digest on the page would expire and the request would fail with a 403 FORBIDDEN result. By default, a request digest token is valid for 30 minutes, so before using it, you have to ensure that it's still valid. In the past you had to do this manually, by comparing the timestamp from the request digest with the current time. SharePoint Framework simplifies this process by offering you two ways of ensuring that your request has a valid request digest token.
1) Use the SPHttpClient to communicate with the SharePoint REST API
2) Retrieve valid request digest using the DigestCache service
If you can't use the SPHttpClient for communicating with the SharePoint REST API, you can obtain a valid request digest token using the DigestCache service provided with the SharePoint Framework. The benefit of using the DigestCache service over manually obtaining a valid request digest token is, that the DigestCache automatically checks if the previously retrieved request digest is still valid or not. If it's expired, the DigestCache service will automatically request a new request digest token from SharePoint and store it from subsequent requests. Using the DigestCache simplifies your code and makes your solution more robust.
To use the DigestCache service in your code, first import the DigestCache and IDigestCache types from the @microsoft/sp-http package:
Next, whenever you need a valid request digest token, retrieve a reference to the DigestCache service and call its fetchDigest method: