<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Disha Gupta</title>
    <description>The latest articles on DEV Community by Disha Gupta (@disha_gupta_f258e3cc18dc6).</description>
    <link>https://dev.to/disha_gupta_f258e3cc18dc6</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3703564%2Fcafabdaf-550a-408e-846a-1ad60ee0bae2.png</url>
      <title>DEV Community: Disha Gupta</title>
      <link>https://dev.to/disha_gupta_f258e3cc18dc6</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/disha_gupta_f258e3cc18dc6"/>
    <language>en</language>
    <item>
      <title>Axios</title>
      <dc:creator>Disha Gupta</dc:creator>
      <pubDate>Thu, 15 Jan 2026 09:06:05 +0000</pubDate>
      <link>https://dev.to/disha_gupta_f258e3cc18dc6/axios-2eio</link>
      <guid>https://dev.to/disha_gupta_f258e3cc18dc6/axios-2eio</guid>
      <description>&lt;p&gt;Axios is a Javascript library used to make HTTP requests from your frontend to your backend API.&lt;br&gt;
Axios is easier and more convenient than built-in fetch API.&lt;/p&gt;

&lt;p&gt;Method                  Purpose&lt;br&gt;
axios.get()             Fetch data&lt;br&gt;
axios.post()            Create/submit data&lt;br&gt;
axios.put()             Update entire resource&lt;br&gt;
axios.patch()           Partial update&lt;br&gt;
axios.delete()          Delete data&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to use Axios in Your Projects&lt;/strong&gt;&lt;br&gt;
Step1: Install Axios&lt;br&gt;
step2: Basic Axios Usage&lt;br&gt;
Here are the common HTTP methods:&lt;br&gt;
`import axios from 'axios'&lt;br&gt;
import axios from 'axios'&lt;/p&gt;

&lt;p&gt;// GET request&lt;br&gt;
axios.get('&lt;a href="http://localhost:3000/api/users'" rel="noopener noreferrer"&gt;http://localhost:3000/api/users'&lt;/a&gt;)&lt;br&gt;
  .then(response =&amp;gt; console.log(response.data))&lt;br&gt;
  .catch(error =&amp;gt; console.log(error))&lt;/p&gt;

&lt;p&gt;// POST request&lt;br&gt;
axios.post('&lt;a href="http://localhost:3000/api/users" rel="noopener noreferrer"&gt;http://localhost:3000/api/users&lt;/a&gt;', {&lt;br&gt;
  name: 'John',&lt;br&gt;
  email: '&lt;a href="mailto:john@example.com"&gt;john@example.com&lt;/a&gt;'&lt;br&gt;
})&lt;br&gt;
  .then(response =&amp;gt; console.log(response.data))&lt;br&gt;
  .catch(error =&amp;gt; console.log(error))&lt;/p&gt;

&lt;p&gt;// PUT request (update)&lt;br&gt;
axios.put('&lt;a href="http://localhost:3000/api/users/1" rel="noopener noreferrer"&gt;http://localhost:3000/api/users/1&lt;/a&gt;', {&lt;br&gt;
  name: 'Jane'&lt;br&gt;
})&lt;br&gt;
  .then(response =&amp;gt; console.log(response.data))&lt;br&gt;
  .catch(error =&amp;gt; console.log(error))&lt;/p&gt;

&lt;p&gt;// DELETE request&lt;br&gt;
axios.delete('&lt;a href="http://localhost:3000/api/users/1'" rel="noopener noreferrer"&gt;http://localhost:3000/api/users/1'&lt;/a&gt;)&lt;br&gt;
  .then(response =&amp;gt; console.log(response.data))&lt;br&gt;
  .catch(error =&amp;gt; console.log(error))&lt;br&gt;
&lt;code&gt;&lt;br&gt;
_Step 3: In React components (with hooks)_&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
import React, { useState, useEffect } from 'react'&lt;br&gt;
import axios from 'axios'&lt;/p&gt;

&lt;p&gt;function UserList() {&lt;br&gt;
  const [users, setUsers] = useState([])&lt;br&gt;
  const [loading, setLoading] = useState(false)&lt;br&gt;
  const [error, setError] = useState(null)&lt;/p&gt;

&lt;p&gt;// Fetch data on component mount&lt;br&gt;
  useEffect(() =&amp;gt; {&lt;br&gt;
    fetchUsers()&lt;br&gt;
  }, [])&lt;/p&gt;

&lt;p&gt;const fetchUsers = async () =&amp;gt; {&lt;br&gt;
    setLoading(true)&lt;br&gt;
    try {&lt;br&gt;
      const response = await axios.get('&lt;a href="http://localhost:3000/api/users'" rel="noopener noreferrer"&gt;http://localhost:3000/api/users'&lt;/a&gt;)&lt;br&gt;
      setUsers(response.data.users)&lt;br&gt;
      setError(null)&lt;br&gt;
    } catch (err) {&lt;br&gt;
      setError(err.message)&lt;br&gt;
    } finally {&lt;br&gt;
      setLoading(false)&lt;br&gt;
    }&lt;br&gt;
  }&lt;/p&gt;

&lt;p&gt;if (loading) return &lt;/p&gt;
&lt;p&gt;Loading...&lt;/p&gt;
&lt;br&gt;
  if (error) return &lt;p&gt;Error: {error}&lt;/p&gt;

&lt;p&gt;return (&lt;br&gt;
    &lt;/p&gt;
&lt;br&gt;
      {users.map(user =&amp;gt; (&lt;br&gt;
        &lt;br&gt;
          &lt;h3&gt;{user.name}&lt;/h3&gt;
&lt;br&gt;
          &lt;p&gt;{user.email}&lt;/p&gt;
&lt;br&gt;
        &lt;br&gt;
      ))}&lt;br&gt;
    &lt;br&gt;
  )&lt;br&gt;
}

&lt;p&gt;export default UserList&lt;br&gt;
&lt;code&gt;&lt;br&gt;
_Step 4: With authentication (sending tokens)_&lt;br&gt;
&lt;/code&gt;axios.post('&lt;a href="http://localhost:3000/api/auth/login" rel="noopener noreferrer"&gt;http://localhost:3000/api/auth/login&lt;/a&gt;',&lt;br&gt;
{email,password},&lt;br&gt;
{&lt;br&gt;
  withCredentials:true, // Send cookies&lt;br&gt;
  headers:{&lt;br&gt;
     'Content-Type':'application/json'&lt;br&gt;
}&lt;br&gt;
}&lt;br&gt;
)`&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Step 5: Create a reusable Axios instance&lt;/em&gt;&lt;br&gt;
create src/api/axiosInstance.js:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import axios from 'axios'
const axiosInstance = axios.create({
baseURL:'http://localhost:3000/api',
withCredentials:true
})

// Add token to all requests
axiosInstance.interceptors.request.use(config =&amp;gt; {
const token = localStorage.getItem('token')
if (token) {
config.headers.Authorization = `Bearer ${token}`
}
return config
})
export default axiosInstance
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then use it in components:&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

import axiosInstance from '../api/axiosInstance'

//Instead of full URL
axiosInstance.post('/auth/login',{email,password})
.then(response =&amp;gt; console.log(response.data))

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
    </item>
  </channel>
</rss>
