Skip to content

Instantly share code, notes, and snippets.



Created Feb 8, 2019
What would you like to do?
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: ''
const body = {
file: new Blob([JSON.stringify(data)], {type: 'application/json'}),
f: 'json',
token: token,
fileName: 'wat.json'
const itemId = 'aed0f8b3c4254097804fc03f7d1aa4b8';
const baseUrl = `${itemId}`;
const postUrl = `${baseUrl}/updateInfo`;
const getUrl = `${itemId}/info/wat.json?f=json&token=${token}`;
opts = {
method: 'POST',
body: encodeForm(body)
fetch(postUrl, opts)
.then(r => r.json())
.then(r => {`Call to add info to ${itemId} returned ${JSON.stringify(r)}`);
return fetch(getUrl);
.then(r => r.json())
.then(r => {`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
You can’t perform that action at this time.