For the put method, we have:
const putMethod = { method: 'PUT', // Method itself headers: { 'Content-type': 'application/json; charset=UTF-8' // Indicates the content }, body: JSON.stringify(someData) // We send data in JSON format } fetch(url, putMethod) .then(response => response.json()) .then(data => console.log(data)) // Manipulate the data retrieved back, if we want to do something with it .catch(err => console.log(err) // Do something with the error
For example, for someData we can have several input fields or all you need:
const someData = { title: document.querySelector(TitleInput).value, body: document.querySelector(BodyInput).value }
And in our data base will be in json format:
{ "posts": [ "id": 1, "title": "Some Title", // what we typed in the title input field "body": "Some Body", // what we typed in the body input field ] }
For the removal method, we have:
const deleteMethod = { method: 'DELETE',
In the URL we need to enter the delete identifier: https://www.someapi/id
Grecdev
source share