moharnadreza / Persianjsonplaceholder
Projects that are alternatives of or similar to Persianjsonplaceholder
PersianJSONPlaceholder
Persian JSONPlaceholder is a simple fake REST API for testing and prototyping.
Read this post on Virgool.io for more information about it.
Why?
Most of the time when trying a new library, hacking a prototype or following a tutorial, I found myself in need of some Persian data, So I decided to make Persian version of https://jsonplaceholder.typicode.com.
You can find it running here and are free to use it in your developments: https://jsonplaceholder.ir.
I hope you will find it useful.
Available resources
Let's start with resources, JSONPlaceholder provides the usual suspects:
- Posts https://jsonplaceholder.ir/posts
- Comments https://jsonplaceholder.ir/comments
- Albums https://jsonplaceholder.ir/albums
- Photos https://jsonplaceholder.ir/photos
- Users https://jsonplaceholder.ir/users
- Todos https://jsonplaceholder.ir/todos
How to
Here's some code using Fetch API showing what can be done with JSONPlaceholder.
Showing a resource
fetch('https://jsonplaceholder.ir/posts/1')
.then(response => response.json())
.then(json => console.log(json))
Listing resources
fetch('https://jsonplaceholder.ir/posts')
.then(response => response.json())
.then(json => console.log(json))
Creating a resource
// POST adds a random id to the object sent
fetch('https://jsonplaceholder.ir/posts', {
method: 'POST',
body: JSON.stringify({
title: 'foo',
body: 'bar',
userId: 1,
}),
headers: {
'Content-type': 'application/json; charset=UTF-8',
},
})
.then(response => response.json())
.then(json => console.log(json))
/* will return
{
id: 100,
title: 'foo',
body: 'bar',
userId: 1
}
*/
Note: these resources will not be really created / updated or deleted on the server but it will be faked as if.
Updating a resource
fetch('https://jsonplaceholder.ir/posts/1', {
method: 'PUT',
body: JSON.stringify({
id: 1,
title: 'foo',
body: 'bar',
userId: 1,
}),
headers: {
'Content-type': 'application/json; charset=UTF-8',
},
})
.then(response => response.json())
.then(json => console.log(json))
/* will return
{
id: 1,
title: 'foo',
body: 'bar',
userId: 1
}
*/
fetch('https://jsonplaceholder.ir/posts/1', {
method: 'PATCH',
body: JSON.stringify({
title: 'foo',
}),
headers: {
'Content-type': 'application/json; charset=UTF-8',
},
})
.then(response => response.json())
.then(json => console.log(json))
/* will return
{
id: 1,
title: 'foo',
body: 'quia et suscipit [...]',
userId: 1
}
*/
Deleting a resource
fetch('https://jsonplaceholder.ir/posts/1', {
method: 'DELETE',
})
Filtering resources
Basic filtering is supported through query parameters.
// Will return all the posts that belong to the first user
fetch('https://jsonplaceholder.ir/posts?userId=1')
.then(response => response.json())
.then(json => console.log(json))
Nested resources
One level of nested route is available.
// equivalent to /comments?postId=1
fetch('https://jsonplaceholder.ir/posts/1/comments')
.then(response => response.json())
.then(json => console.log(json))
Here's the list of available nested routes:
- https://jsonplaceholder.ir/posts/1/comments
- https://jsonplaceholder.ir/albums/1/photos
- https://jsonplaceholder.ir/users/1/albums
- https://jsonplaceholder.ir/users/1/todos
- https://jsonplaceholder.ir/users/1/posts
Use JSONPlaceholder with GraphQL
All of the requests are available in https://jsonplaceholder.ir/graphql with different queries, get needed params for each one :)
// For example:get all data for one topic
let queryAllPosts = '{ posts { title body } }'
let queryAllComments = '{ comments { name email } }'
let queryAllAlbums = '{ albums { userId title } }'
let queryAllPhotos = '{ photos { title url } }'
let queryAllTodos = '{ todos { title completed } }'
let queryAllUsers = '{ users { username email } }'
// For example:get specific data by id
let queryOnePost = '{ post(id:2) { title body } }'
let queryOneComment = '{ comment(id:3) { name email } }'
let queryOneAlbum = '{ album(id:4) { userId title } }'
let queryOnePhoto = '{ photo(id:5) { title url } }'
let queryOneTodo = '{ todo(id:6) { title completed } }'
let queryOneUser = '{ user(id:7) { username email } }'
// and ...
// Or use request package to fetch data as JSON file format
const request = require('request')
request(
{
method: 'POST',
url: 'https://jsonplaceholder.ir/graphql',
json: {
query: queryOnePhoto,
},
},
(err, res, data) => {
console.log(data)
}
)
// Fetch data as JSON file format
fetch('https://jsonplaceholder.ir/graphql', {
method: 'POST',
body: JSON.stringify(queryOneUser),
headers: {
'Content-type': 'application/json; charset=UTF-8',
},
})
.then(response => response.json())
.then(json => console.log(json))