Skip to content

Instantly share code, notes, and snippets.

@mortyobnoxious
Created January 29, 2023 19:52
Show Gist options
  • Save mortyobnoxious/1e4f5de6afd617fc4e65181b64c05fb3 to your computer and use it in GitHub Desktop.
Save mortyobnoxious/1e4f5de6afd617fc4e65181b64c05fb3 to your computer and use it in GitHub Desktop.
jsonblob.com - by ChatGPT
async function requestBlob(method, id, json) {
let url = 'https://jsonblob.com/api/jsonBlob';
let body;
if (id) {url += `/${id}`;}
if (json) {body = JSON.stringify(json);}
const response = await fetch(url, {
method,
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
},
body,
});
if (response.ok) {
if (method === 'DELETE') {return;}
else if (method === 'POST') {return response.headers.get('Location')}
else {return await response.json();}
} else {throw new Error('Request failed!');}
}
// Create a new blob with JSON content
requestBlob('POST', null, { foo: 'bar' }).then((location) => {
console.log(`Blob created at ${JSON.stringify(location)}`);
});
// Get the JSON content of a blob with ID 'abcdef'
requestBlob('GET', 'abcdef').then((json) => {
console.log(`Retrieved JSON: ${json}`);
});
// Update a blob with ID 'abcdef' with new JSON content
requestBlob('PUT', 'abcdef', { foo: 'baz' }).then((json) => {
console.log(`Updated JSON: ${json}`);
});
// Delete a blob with ID 'abcdef'
requestBlob('DELETE', 'abcdef').then(() => {
console.log('Blob deleted');
});
// Retrieve the existing data from the blob
requestBlob('GET', 'abcdef').then((json) => {
// Update the data with new content
json.newKey = 'newValue';
// Update the blob with the modified data
requestBlob('PUT', 'abcdef', json).then((json) => {
console.log(`Updated JSON: ${json}`);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment