iMohammadReza / PersianJSONPlaceholder

Persian Fake Online REST API for Testing and Prototyping

Home Page:https://jsonplaceholder.ir

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Persian JSONPlaceholder

Persian JSONPlaceholder is a simple Persian 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:

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: 101,
  title: 'foo',
  body: 'bar',
  userId: 1
}
*/

Note: the resource will not be really created 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
}
*/

Note: the resource will not be really updated on the server but it will be faked as if.

Deleting a resource

fetch('https://jsonplaceholder.ir/posts/1', {
  method: 'DELETE'
})

Note: the resource will not be really deleted on the server but it will be faked as if.

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:

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 ...



// 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)
})



// Or use request package to 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))

ToDo

  • Add pagination

About

Persian Fake Online REST API for Testing and Prototyping

https://jsonplaceholder.ir

License:MIT License


Languages

Language:JavaScript 100.0%