Skip to content

Instantly share code, notes, and snippets.

@shah0150
Created November 13, 2023 23:45
Show Gist options
  • Select an option

  • Save shah0150/b0f473a7231583734e845014caf61580 to your computer and use it in GitHub Desktop.

Select an option

Save shah0150/b0f473a7231583734e845014caf61580 to your computer and use it in GitHub Desktop.
let apiUrl = 'https://jsonplaceholder.typicode.com/todos';
// Data to be uploaded
let postData = {
title: 'Read materials for JS',
completed: false
}
// Options or headers
let options = {
method: 'POST', //using POST to upload my data
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(postData)
};
// Make the fetch request
fetch(apiUrl, options)
.then(response => {
if (!response.ok){
throw new Error('HTTP Error. Status: ${response.status}');
}
return response.json() // parse the response as JSON
})
.then(data => {
console.log("Data successfully uploaded: ", data);
}).catch(error => {
console.log("Error uploading data: ", error);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment