Skip to content

Instantly share code, notes, and snippets.

@dbouwman
Created February 8, 2019 19:57
Show Gist options
  • Save dbouwman/df947dc9f127efc64d7e10bec2cb1a99 to your computer and use it in GitHub Desktop.
Save dbouwman/df947dc9f127efc64d7e10bec2cb1a99 to your computer and use it in GitHub Desktop.
CodePen Hacking of /updateInfo
const token = 'FbIWugy4eCsS-hq2ZV42MJR7EYH9ID_AXY2SAeNmCZvi5NVjSNuUysrCr1cgesIZHP_ti31IaGWLZDJFX1U21Z3aQ3B6A8Xcny9kZc-i_IdUzY-kBYXyV2gn_euATqpfr8zw9TemIODV4ORAB0gxDPTwc884PpR_qVwa4Ud-nc47JoN_v53_n1xzSqSX6bg_OSy7VDcevFyiA8hOpWdekg..';
const data = {
name: 'Dave Bouwman',
email: '[email protected]'
};
const body = {
file: new Blob([JSON.stringify(data)], {type: 'application/json'}),
f: 'json',
token: token,
fileName: 'wat.json'
}
const itemId = 'aed0f8b3c4254097804fc03f7d1aa4b8';
const baseUrl = `https://dc.mapsqa.arcgis.com/sharing/rest/content/users/dcadminqa/items/${itemId}`;
const postUrl = `${baseUrl}/updateInfo`;
const getUrl = `https://dc.mapsqa.arcgis.com/sharing/rest/content/items/${itemId}/info/wat.json?f=json&token=${token}`;
opts = {
method: 'POST',
body: encodeForm(body)
}
fetch(postUrl, opts)
.then(r => r.json())
.then(r => {
console.info(`Call to add info to ${itemId} returned ${JSON.stringify(r)}`);
return fetch(getUrl);
})
.then(r => r.json())
.then(r => {
console.info(`Call to get the file returned ${JSON.stringify(r)}`);
})
function encodeForm(newParams) {
const formData = new FormData();
Object.keys(newParams).forEach((key: any) => {
if (typeof Blob !== "undefined" && newParams[key] instanceof Blob) {
/* To name the Blob:
1. look to an alternate request parameter called 'fileName'
2. see if 'name' has been tacked onto the Blob manually
3. if all else fails, use the request parameter
*/
const filename = newParams["fileName"] || newParams[key].name || key;
formData.append(key, newParams[key], filename);
} else {
formData.append(key, newParams[key]);
}
});
return formData;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment