DEV Community

levin-achieng-dieto
levin-achieng-dieto

Posted on

Interacting with the json server

veterinary clinic is an app that helps veterinarian to register their customers pets and the information about the registered pet are stored in private API. The interactions that exist between the input and the stored data is still a magic to me and nothing fascinates more than knowing i can keep tracks of the inputs and be able to retrieve the data and use them later

when a POST request is done to the server, the information is sent and this is how we are able to let the server know that the information is to be added to the existing information. Below is a snippet of code that shows the information that should be sent to the server.

let catObject = {
catname:e.target['pet_name'].value,
age:e.target['pet_age'].value,
image:e.target['pet_image'].value
}

POST made it easier for me to capture the above information and store the my json server. below is a snippet of how i was able to send the information

function renderCat(catObject){
    fetch('http://localhost:3000/cats', {
        method: 'POST',
        headers: {
            'Content-Type':'application/json',
            Accept: "application/json"
        },

        body:JSON.stringify(catObject)
    })
    .then(response => response.json())
    .then(data => console.log(data))
    }
Enter fullscreen mode Exit fullscreen mode

The code above tells the server to look for the end point called cat and add the objects that are inside the cat object to the server.

Top comments (0)