PUT Request Documentation

Learn how to make PUT requests using our Postboy

What is a PUT Request?

The HTTP PUT method is used to update or replace an existing resource on the server. Unlike POST, which typically creates a new resource, PUT updates a resource at a specific URI.

Key characteristics of PUT requests:

PUT vs POST vs PATCH

PUT - Replaces the entire resource at a specific URI. Use when you want to update the complete resource.

POST - Creates a new resource. The server typically determines the URI for the new resource.

PATCH - Partially updates a resource. Only the fields you send are modified.

How to Make a PUT Request

Using our Postboy, follow these steps to make a PUT request:

  1. Select PUT from the method dropdown
  2. Enter the URL of the resource you want to update
  3. Go to the "Body" tab to add your request data
  4. Select the appropriate body format:
    • Form Data: For files and binary data
    • x-www-form-urlencoded: For form submissions
    • Raw JSON: For sending JSON data (most common)
  5. Add headers in the "Headers" tab (e.g., Content-Type)
  6. Click the "Send" button

The response will appear in the response section below.

Example PUT Requests

Example 1: Update a Resource with JSON

HTTP
PUT https://jsonplaceholder.typicode.com/posts/1 HTTP/1.1
Host: jsonplaceholder.typicode.com
Content-Type: application/json

{
  "id": 1,
  "title": "Updated Title",
  "body": "Updated content for this post",
  "userId": 1
}

Response:

JSON
{
  "id": 1,
  "title": "Updated Title",
  "body": "Updated content for this post",
  "userId": 1
}

Example 2: Update User Profile

HTTP
PUT https://api.example.com/users/123 HTTP/1.1
Host: api.example.com
Content-Type: application/json
Authorization: Bearer your_token_here

{
  "id": 123,
  "name": "John Doe",
  "email": "john.doe@example.com",
  "age": 30,
  "city": "New York"
}

Response:

JSON
{
  "id": 123,
  "name": "John Doe",
  "email": "john.doe@example.com",
  "age": 30,
  "city": "New York",
  "updatedAt": "2024-01-15T10:30:00Z"
}

Example 3: Replace Product Information

HTTP
PUT https://api.store.com/products/456 HTTP/1.1
Host: api.store.com
Content-Type: application/json

{
  "id": 456,
  "name": "Wireless Headphones",
  "price": 99.99,
  "description": "Premium wireless headphones with noise cancellation",
  "inStock": true,
  "category": "Electronics"
}

Response:

JSON
{
  "id": 456,
  "name": "Wireless Headphones",
  "price": 99.99,
  "description": "Premium wireless headphones with noise cancellation",
  "inStock": true,
  "category": "Electronics",
  "lastUpdated": "2024-01-15T10:30:00Z"
}

Common HTTP Status Codes for PUT Requests

Best Practices for PUT Requests

Try It in JavaScript

Here's how you can make a PUT request in JavaScript:

JavaScript Example

JavaScript
// Using fetch API with JSON data
fetch('https://jsonplaceholder.typicode.com/posts/1', {
  method: 'PUT',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    id: 1,
    title: 'Updated Title',
    body: 'Updated content for this post',
    userId: 1
  })
})
.then(response => {
  if (!response.ok) {
    throw new Error('Network response was not ok');
  }
  return response.json();
})
.then(data => console.log('Resource updated:', data))
.catch(error => console.error('Error:', error));

// Using async/await
async function updateResource(id, data) {
  try {
    const response = await fetch(`https://api.example.com/posts/${id}`, {
      method: 'PUT',
      headers: {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer your_token_here'
      },
      body: JSON.stringify(data)
    });
    
    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`);
    }
    
    const updatedData = await response.json();
    console.log('Resource updated successfully:', updatedData);
    return updatedData;
  } catch (error) {
    console.error('Error updating resource:', error);
    throw error;
  }
}

// Example usage
const updatedPost = {
  id: 1,
  title: 'My Updated Post',
  body: 'This is the updated content',
  userId: 1
};

updateResource(1, updatedPost);