DEV Community

Cover image for Making PUT & DELETE Request Using Axios In React.js
Aya Bouchiha
Aya Bouchiha

Posted on

8 4 1

Making PUT & DELETE Request Using Axios In React.js

Hi, I'm Aya Bouchiha, today, we'll cover sending PUT and DELETE requests in react.js using axios.

Axios

axios: is a popular Javascript library used for making HTTP requests to an API.

Why axios instead of fetch?

I recommend reading this article by Faraz Kelhini :

Axios installation

cdn

<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
Enter fullscreen mode Exit fullscreen mode

Or:

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
Enter fullscreen mode Exit fullscreen mode

npm

npm i axios
Enter fullscreen mode Exit fullscreen mode

yarn

yarn add axios
Enter fullscreen mode Exit fullscreen mode

bower

bower install axios
Enter fullscreen mode Exit fullscreen mode

PUT request using axios

PUT: is a request used for creating or updating a resource in a specific server.

Code using then and catch

import { useEffect } from 'react';
import axios from 'axios';

const todo = {
    id: 10,
    title: 'go to gym',
    body: 'practicing sport is very important',
    userId: 2,
};

const App = () => {
    useEffect(() => {
        axios
            .put('https://jsonplaceholder.typicode.com/posts/10', todo)
            .then((response) => {
                console.log(response.status);
                console.log(response.data);
            })
            .catch((e) => console.log('something went wrong :(', e));
    }, []);
    return <div>PUT REQUEST</div>;
};
export default App;
Enter fullscreen mode Exit fullscreen mode

Console

200
{id: 10, title: "go to gym", body: "practicing sport is very important", userId: 2}
Enter fullscreen mode Exit fullscreen mode

Code using async and await

import { useEffect } from 'react';
import axios from 'axios';

const todo = {
    id: 10,
    title: 'go to gym',
    body: 'practicing sport is very important',
    userId: 2,
};

const App = () => {
    useEffect(() => {
        const putTodo = async () => {
            try {
                const response = await axios.put(
                    'https://jsonplaceholder.typicode.com/posts/10',
                    todo,
                );
                console.log(response.status);
                console.log(response.data);
            } catch (e) {
                console.log('something went wrong :(', e);
            }
        };
        putTodo();
    }, []);
    return <div>PUT REQUEST</div>;
};
export default App;
Enter fullscreen mode Exit fullscreen mode

Console

200
{id: 10, title: "go to gym", body: "practicing sport is very important", userId: 2}
Enter fullscreen mode Exit fullscreen mode

DELETE request using axios

DELETE: is a request used to delete a specific resource in a server.

Code using then and catch

import { useEffect } from 'react';
import axios from 'axios';

const App = () => {
    useEffect(() => {
        axios
            .delete('https://jsonplaceholder.typicode.com/posts/10')
            .then((response) => {
                console.log(response.status);
            })
            .catch((e) => console.log('something went wrong!', e));
    }, []);
    return <div>DELETE REQUEST</div>;
};
export default App;
Enter fullscreen mode Exit fullscreen mode

console

200
Enter fullscreen mode Exit fullscreen mode

Code using async and await

import { useEffect } from 'react';
import axios from 'axios';

const App = () => {
    useEffect(() => {
        const deleteTodo = async () => {
            try {
                const response = await axios.delete(
                    'https://jsonplaceholder.typicode.com/posts/10',
                );
                console.log(response.status);
            } catch (e) {
                console.log('something went wrong!', e);
            }
        };
        deleteTodo();
    }, []);
    return <div>DELETE REQUEST</div>;
};
export default App;
Enter fullscreen mode Exit fullscreen mode

console

200
Enter fullscreen mode Exit fullscreen mode

Useful Resources

Suggested Posts

To Contact Me:

Happy codding!

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (1)

Collapse
 
suraj__ profile image
Suraj

📚

Cloudinary image

Optimize, customize, deliver, manage and analyze your images.

Remove background in all your web images at the same time, use outpainting to expand images with matching content, remove objects via open-set object detection and fill, recolor, crop, resize... Discover these and hundreds more ways to manage your web images and videos on a scale.

Learn more

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay