Posts
The Posts resource represents blog posts with title and content. Each post belongs to a user and can have multiple comments.
Base URL
bash
https://api.jsonplaceholder.dev
bash
http://localhost:3000
Schema
typescript
interface Post {
id: number
userId: number
title: string
body: string
}
Endpoints
Get All Posts
bash
GET /posts
Response:
json
[
{
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
}
// ... 99 more posts
]
Get Posts by User
bash
GET /posts?userId={userId}
Parameters:
userId
(number, query) - Filter posts by user ID
Response:
json
[
{
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
}
// ... more posts by user 1
]
Get Single Post
bash
GET /posts/{id}
Parameters:
id
(number) - Post ID
Response:
json
{
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
}
Get Post Comments
bash
GET /posts/{id}/comments
Parameters:
id
(number) - Post ID
Response:
json
[
{
"postId": 1,
"id": 1,
"name": "id labore ex et quam laborum",
"email": "[email protected]",
"body": "laudantium enim quasi est quidem magnam voluptate ipsam eos\ntempora quo necessitatibus\ndolor quam autem quasi\nreiciendis et nam sapiente accusantium"
}
// ... more comments for this post
]
Create Post
bash
POST /posts
Request Body:
json
{
"userId": 1,
"title": "My New Post",
"body": "This is the content of my new post"
}
Response: 201 Created
json
{
"id": 101,
"userId": 1,
"title": "My New Post",
"body": "This is the content of my new post"
}
Update Post (Full Replace)
bash
PUT /posts/{id}
Parameters:
id
(number) - Post ID
Request Body:
json
{
"userId": 1,
"title": "Updated Post Title",
"body": "Updated post content"
}
Response: 200 OK
json
{
"id": 1,
"userId": 1,
"title": "Updated Post Title",
"body": "Updated post content"
}
Update Post (Partial)
bash
PATCH /posts/{id}
Parameters:
id
(number) - Post ID
Request Body: Partial post object
json
{
"title": "New Title Only"
}
Response: 200 OK
- Updated post object
Delete Post
bash
DELETE /posts/{id}
Parameters:
id
(number) - Post ID
Response: 200 OK
json
{}
Examples
javascript
// Get all posts
const posts = await fetch('https://api.jsonplaceholder.dev/posts')
.then(res => res.json());
// Get posts by user
const userPosts = await fetch('https://api.jsonplaceholder.dev/posts?userId=1')
.then(res => res.json());
// Create new post
const newPost = await fetch('https://api.jsonplaceholder.dev/posts', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
userId: 1,
title: 'My Amazing Post',
body: 'This is the content of my amazing post!'
})
}).then(res => res.json());
// Update post
const updatedPost = await fetch('https://api.jsonplaceholder.dev/posts/1', {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
title: 'Updated Title'
})
}).then(res => res.json());
bash
# Get all posts
curl https://api.jsonplaceholder.dev/posts
# Get posts by user
curl https://api.jsonplaceholder.dev/posts?userId=1
# Create new post
curl -X POST https://api.jsonplaceholder.dev/posts \
-H "Content-Type: application/json" \
-d '{
"userId": 1,
"title": "My Amazing Post",
"body": "This is the content of my amazing post!"
}'
# Update post
curl -X PATCH https://api.jsonplaceholder.dev/posts/1 \
-H "Content-Type: application/json" \
-d '{"title": "Updated Title"}'
# Delete post
curl -X DELETE https://api.jsonplaceholder.dev/posts/1
python
import requests
# Get all posts
posts = requests.get('https://api.jsonplaceholder.dev/posts').json()
# Get posts by user
user_posts = requests.get('https://api.jsonplaceholder.dev/posts?userId=1').json()
# Create new post
new_post = requests.post('https://api.jsonplaceholder.dev/posts', json={
'userId': 1,
'title': 'My Amazing Post',
'body': 'This is the content of my amazing post!'
}).json()
# Update post
updated_post = requests.patch('https://api.jsonplaceholder.dev/posts/1', json={
'title': 'Updated Title'
}).json()
# Delete post
requests.delete('https://api.jsonplaceholder.dev/posts/1')