HTTP 201 Created
Overview
The HTTP 201 Created
status code is used when a request has been successfully processed and has resulted in the creation of a new resource. This status code is often seen in response to POST or PUT requests in web applications and APIs.
Purpose
The primary purpose of the HTTP 201 response is to inform the client about the successful creation of a resource. It’s commonly used in RESTful API interactions to indicate that a new entity (like a user record, a file, or a database entry) has been successfully created on the server.
Usage
Client Behavior:
- Send Request: The client sends an HTTP POST or PUT request to the server to create a new resource.
- Receive Response: The client receives the HTTP 201 status code, indicating that the resource was created successfully.
Server Behavior:
- Process Request: The server processes the request and creates a new resource.
- Send Response: The server sends a
201 Created
response, often including aLocation
header indicating the URI of the new resource.
Scenarios
- Creating New Resources: When a client sends data to create a new resource, like a new user account, blog post, or file upload.
- REST API Operations: In REST APIs, returning a
201 Created
for successful POST requests that result in the creation of a new resource.
Sequence Diagram
sequenceDiagram participant Client participant Server as Web Server Note over Client: Step 1: Client sends a POST/PUT request to create a resource Client->>Server: POST /new-item HTTP/1.1 (Step 1) Note over Server: Step 2: Server processes the request and creates the resource Server->>Client: HTTP/1.1 201 Created (Step 2) Server->>Client: Location: /new-item/123 (Step 3)
Curl Request and Response Example for HTTP 201 Created
When creating a new resource on a server, such as posting data to a web service or API, you might receive an HTTP 201 Created
response if the resource is successfully created. Here’s an example of how to use Curl to make such a request:
# Sample curl request for creating a new resource
curl -i -X POST -H "Content-Type: application/json" -d '{"key": "value"}' http://example.com/api/resource
# Expected response for successful resource creation
HTTP/1.1 201 Created
Location: /api/resource/123
PHP cURL Request and Response Example for HTTP 201 Created
<?php
// Initialize cURL session
$ch = curl_init('http://example.com/api/resource');
// Data to be sent
$postData = ['key' => 'value'];
$postDataJson = json_encode($postData);
// Set cURL options
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postDataJson);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
// Execute cURL session
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// Check for HTTP 201 Created response
if ($httpCode == 201) {
echo "Resource created successfully.\n";
// Extract and display Location header (if available)
$headers = substr($response, 0, curl_getinfo($ch, CURLINFO_HEADER_SIZE));
if (preg_match('/Location: (.*)\n/', $headers, $matches)) {
echo "Location of new resource: " . trim($matches[1]) . "\n";
}
} else {
echo "Failed to create resource, status code: " . $httpCode . "\n";
}
// Close cURL session
curl_close($ch);
?>
Python Request and Response Example for HTTP 201 Created
import requests
# URL for creating a new resource
url = 'http://example.com/api/resource'
data = {'key': 'value'}
# Sending a POST request
response = requests.post(url, json=data)
# Checking for HTTP 201 Created response
if response.status_code == 201:
print("Resource created successfully.")
print("Location of new resource:", response.headers.get('Location'))
else:
print(f"Failed to create resource, status code: {response.status_code}")
Apache Configuration for HTTP 100 Continue
# Apache Configuration Example
<VirtualHost *:80>
ServerName example.com
# Enable KeepAlive for better handling of Expect: 100-continue
KeepAlive On
# Other directives...
<Location "/upload">
# Adjust the client request body size limit (e.g., 10M)
LimitRequestBody 10485760
# Additional location-specific directives...
</Location>
</VirtualHost>
NGINX Configuration for HTTP 201 Created
# NGINX Configuration Example
server {
listen 80;
server_name example.com;
location /api/resource {
# Assuming NGINX acts as a reverse proxy for an application server
proxy_pass http://backend_application_server;
# Additional directives for handling POST requests
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# Other configurations...
}
}