Created
December 21, 2021 10:54
-
-
Save marcusschiesser/2ded05c27411a81d61d327e1ed6fdc8a to your computer and use it in GitHub Desktop.
POST request from a Splunk app using the @splunk/splunk-utils package
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
import * as config from '@splunk/splunk-utils/config'; | |
import { createRESTURL } from '@splunk/splunk-utils/url'; | |
import { handleError, handleResponse } from '@splunk/splunk-utils/fetch'; | |
... | |
const url = createRESTURL( | |
'storage/collections/data/mycollection/key', | |
{ app: config.app, sharing: 'app' } | |
); | |
fetch(url, { | |
method: 'POST', | |
credentials: 'include', | |
headers: { | |
'X-Splunk-Form-Key': config.CSRFToken, | |
'X-Requested-With': 'XMLHttpRequest', | |
'Content-Type': 'application/json', | |
}, | |
body: JSON.stringify(data), | |
}).then(handleResponse(200)) | |
.then((json) => { | |
// success case - do something | |
}, handleError('Error updating. Please try again.')) | |
.catch((errMsg) => { | |
// error case - print `errMsg` to the user | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment