← Back to Chapters

React Native PUT API

? React Native PUT API

? Quick Overview

In React Native, a PUT API request is used to update an existing resource on a server. It commonly replaces the entire object with new data and is widely used in RESTful APIs.

? Key Concepts

  • PUT is an HTTP method for updating data
  • Usually sends data in JSON format
  • Commonly used with fetch() or Axios
  • Requires headers like Content-Type

? Syntax / Theory

A PUT request sends updated data to a specific endpoint. The server identifies the resource using an ID in the URL.

? View Code Example
// Basic structure of a PUT request using fetch
fetch("https://example.com/api/users/1", {
  method: "PUT",
  headers: {
    "Content-Type": "application/json"
  },
  body: JSON.stringify({
    name: "John Doe",
    email: "john@example.com"
  })
});

? Code Example (React Native)

? View Code Example
// Updating user data using PUT API in React Native
const updateUser = async () => {
  const response = await fetch("https://jsonplaceholder.typicode.com/users/1", {
    method: "PUT",
    headers: {
      "Content-Type": "application/json"
    },
    body: JSON.stringify({
      name: "Updated Name",
      email: "updated@email.com"
    })
  });
  const data = await response.json();
  console.log(data);
};

? Live Output / Explanation

The server receives the updated object and responds with the modified data. In most APIs, the full updated resource is returned as JSON.

? Interactive Example

Imagine a profile edit screen on a mobile device where the user updates their name and email. Pressing the Save button triggers a PUT request to update the profile on the server.

? View Code Example
// Button click triggers PUT request
<Button title="Save Profile" onPress={updateUser} />

? App Simulator: Edit Profile

HTTP 200 OK
// JSON Response will appear here...

? Use Cases

  • Updating user profiles
  • Editing form-based data
  • Admin dashboards
  • Settings and preferences updates

✅ Tips & Best Practices

  • Always send proper headers
  • Handle API errors using try/catch
  • Use PUT for full updates, PATCH for partial updates
  • Validate data before sending

? Try It Yourself

  • Create a PUT API call to update a product
  • Log the server response
  • Add error handling with try/catch
  • Show success message after update