I have an HTML form that I am trying to send (POST). And I keep getting the following 403 error: The security validation for this page is invalid and might be corrupted. Please use your web browser's Back button to try your operation again.
I have read several other posts and tried the accepted answer (make another request before my Post to _api/contextInfo
and I continue to get the same error
value: "The security validation for this page is invalid and might be corrupted. Please use your web browser's Back button to try your operation again."
Here is my updated script per Dylan Cristy's answer:
fetch("/_api/contextinfo", {
method: "POST",
headers: {
"Accept": "application/json;odata=verbose",
}
}).then(contextResponse => {
// get the form digest value that you _would_ have gotten from $('#__REQUESTDIGEST').val()
var requestDigest = contextResponse.GetContextWebInformation.FormDigestValue;
// now use it in your next request
fetch("/_api/web/lists/getbytitle(LeaveRequestPost)/items", {
method: "POST",
credentials: 'same-origin',
body: JSON.stringify(item),
headers: {
"Accept": "application/json; odata=nometadata", // or odata=verbose|minimalmetadata (minimalmetadata is default if odata not specified)
"Content-Type": "application/json; odata=nometadata", // or odata=verbose|minimalmetadata (minimalmetadata is default if odata not specified)
"X-RequestDigest": requestDigest
}
});
});
UPDATE
Now I am getting the following issues:
Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'FormDigestValue').
The response in the Network Tab shows as contextInfo and contains the value shown below, but when I console.log contextResponse, it does not provide .GetContextWebInformation.
{
"d": {
"GetContextWebInformation": {
"__metadata": {
"type": "SP.ContextWebInformation"
},
"FormDigestTimeoutSeconds": 1800,
"FormDigestValue": "FormDigestValue_Value",
"LibraryVersion": "LibraryVersionValue",
"SiteFullUrl": "SiteFullUrlValue",
"SupportedSchemaVersions": {
"__metadata": {
"type": "Collection(Edm.String)"
},
"results": [
"14.0.0.0",
"15.0.0.0"
]
},
"WebFullUrl": "WebFullUrlValue"
}
}
}