Skip to content

Instantly share code, notes, and snippets.

@marcusschiesser
Created December 21, 2021 10:54
Show Gist options
  • Save marcusschiesser/2ded05c27411a81d61d327e1ed6fdc8a to your computer and use it in GitHub Desktop.
Save marcusschiesser/2ded05c27411a81d61d327e1ed6fdc8a to your computer and use it in GitHub Desktop.
POST request from a Splunk app using the @splunk/splunk-utils package
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