Skip to content

Commit 6b9b30e

Browse files
authored
Update README.md
1 parent f768212 commit 6b9b30e

File tree

1 file changed

+2
-152
lines changed

1 file changed

+2
-152
lines changed

README.md

Lines changed: 2 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -34,156 +34,6 @@ I hope you will find it useful.
3434
* HTTP or HTTPS
3535
* Compatible with React, Angular, Vue, Ember, ...
3636

37-
## Available resources
37+
## Guide
3838

39-
Let's start with resources, JSONPlaceholder provides the usual suspects:
40-
41-
* Posts https://jsonplaceholder.typicode.com/posts/1
42-
* Comments https://jsonplaceholder.typicode.com/comments/1
43-
* Albums https://jsonplaceholder.typicode.com/albums/1
44-
* Photos https://jsonplaceholder.typicode.com/photos/1
45-
* Users https://jsonplaceholder.typicode.com/users/1
46-
* Todos https://jsonplaceholder.typicode.com/todos/1
47-
48-
## How to
49-
50-
Here's some code using [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) showing what can be done with JSONPlaceholder.
51-
52-
### Showing a resource
53-
54-
```js
55-
fetch('https://jsonplaceholder.typicode.com/posts/1')
56-
.then(response => response.json())
57-
.then(json => console.log(json))
58-
```
59-
60-
### Listing resources
61-
62-
```js
63-
fetch('https://jsonplaceholder.typicode.com/posts')
64-
.then(response => response.json())
65-
.then(json => console.log(json))
66-
```
67-
68-
### Creating a resource
69-
70-
```js
71-
// POST adds a random id to the object sent
72-
fetch('https://jsonplaceholder.typicode.com/posts', {
73-
method: 'POST',
74-
body: JSON.stringify({
75-
title: 'foo',
76-
body: 'bar',
77-
userId: 1
78-
}),
79-
headers: {
80-
"Content-type": "application/json; charset=UTF-8"
81-
}
82-
})
83-
.then(response => response.json())
84-
.then(json => console.log(json))
85-
86-
/* will return
87-
{
88-
id: 101,
89-
title: 'foo',
90-
body: 'bar',
91-
userId: 1
92-
}
93-
*/
94-
```
95-
96-
Note: the resource will not be really created on the server but it will be faked as if.
97-
98-
### Updating a resource
99-
100-
```js
101-
fetch('https://jsonplaceholder.typicode.com/posts/1', {
102-
method: 'PUT',
103-
body: JSON.stringify({
104-
id: 1,
105-
title: 'foo',
106-
body: 'bar',
107-
userId: 1
108-
}),
109-
headers: {
110-
"Content-type": "application/json; charset=UTF-8"
111-
}
112-
})
113-
.then(response => response.json())
114-
.then(json => console.log(json))
115-
116-
/* will return
117-
{
118-
id: 1,
119-
title: 'foo',
120-
body: 'bar',
121-
userId: 1
122-
}
123-
*/
124-
```
125-
126-
```js
127-
fetch('https://jsonplaceholder.typicode.com/posts/1', {
128-
method: 'PATCH',
129-
body: JSON.stringify({
130-
title: 'foo'
131-
}),
132-
headers: {
133-
"Content-type": "application/json; charset=UTF-8"
134-
}
135-
})
136-
.then(response => response.json())
137-
.then(json => console.log(json))
138-
139-
/* will return
140-
{
141-
id: 1,
142-
title: 'foo',
143-
body: 'quia et suscipit [...]',
144-
userId: 1
145-
}
146-
*/
147-
```
148-
149-
Note: the resource will not be really updated on the server but it will be faked as if.
150-
151-
### Deleting a resource
152-
153-
```js
154-
fetch('https://jsonplaceholder.typicode.com/posts/1', {
155-
method: 'DELETE'
156-
})
157-
```
158-
159-
Note: the resource will not be really deleted on the server but it will be faked as if.
160-
161-
### Filtering resources
162-
163-
Basic filtering is supported through query parameters.
164-
165-
```js
166-
// Will return all the posts that belong to the first user
167-
fetch('https://jsonplaceholder.typicode.com/posts?userId=1')
168-
.then(response => response.json())
169-
.then(json => console.log(json))
170-
```
171-
172-
### Nested resources
173-
174-
One level of nested route is available.
175-
176-
```js
177-
// equivalent to /comments?postId=1
178-
fetch('https://jsonplaceholder.typicode.com/posts/1/comments')
179-
.then(response => response.json())
180-
.then(json => console.log(json))
181-
```
182-
183-
Here's the list of available nested routes:
184-
185-
* https://jsonplaceholder.typicode.com/posts/1/comments
186-
* https://jsonplaceholder.typicode.com/albums/1/photos
187-
* https://jsonplaceholder.typicode.com/users/1/albums
188-
* https://jsonplaceholder.typicode.com/users/1/todos
189-
* https://jsonplaceholder.typicode.com/users/1/posts
39+
For examples and more, you can visit https://jsonplaceholder.typicode.com

0 commit comments

Comments
 (0)