-
-
Notifications
You must be signed in to change notification settings - Fork 578
/
guide.html
286 lines (269 loc) · 9.17 KB
/
guide.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no"
/>
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/modern-normalize/0.5.0/modern-normalize.min.css"
/>
<link rel="stylesheet" href="./style.css" />
<link
rel="stylesheet"
href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/github.min.css"
/>
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>
<link
rel="stylesheet"
href="https://use.fontawesome.com/releases/v5.5.0/css/all.css"
integrity="sha384-B4dIYHKNBt8Bc12p+WXckhzcICo0wtJAoU8YZTY5qE0Id1GSseTk6S+L3BlXeVIU"
crossorigin="anonymous"
/>
<script>
hljs.initHighlightingOnLoad()
</script>
<title>JSONPlaceholder - Fake online REST API for developers</title>
</head>
<body>
<header>
<div class="announcement">
This service is maintained and provided for free, please consider
supporting it on
<a href="https://github.com/users/typicode/sponsorship"
>GitHub Sponsors</a
>
❤️
</div>
<nav class="container">
<ul>
<li><a href="/">JSONPlaceholder</a></li>
<li><a href="/guide.html">Guide</a></li>
<li><a href="https://github.com/typicode">GitHub</a></li>
<li>
<a href="https://my-json-server.typicode.com">My JSON Server</a>
</li>
</ul>
</nav>
</header>
<div class="container">
<p><main></p>
<h2 id="guide">Guide</h2>
<p>You can use JSONPlaceholder with any type of project that needs to get JSON data (React, Vue, Node, Rails, Swift, Android, …).</p>
<p>Below you'll find examples using <a href="https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API">Fetch API</a>. You can copy paste them in your browser Console to quickly test JSONPlaceholder.</p>
<h3 id="getaresource">Get a resource</h3>
<pre><code class="js language-js">fetch('https://jsonplaceholder.typicode.com/posts/1')
.then(response => response.json())
.then(json => console.log(json))
// Output
{
id: 1,
title: '[...]',
body: '[...]',
userId: 1
}
</code></pre>
<div id="codefund"><!-- fallback content --></div>
<script src="https://app.codefund.io/properties/338/funder.js" async="async"></script>
<h3 id="listallresources">List all resources</h3>
<pre><code class="js language-js">fetch('https://jsonplaceholder.typicode.com/posts')
.then(response => response.json())
.then(json => console.log(json))
// Output
[
{ id: 1, title: '[...]' /* ... */ },
/* ... */
{ id: 100, title: '[...]' /* ... */ }
]
</code></pre>
<h3 id="createaresource">Create a resource</h3>
<pre><code class="js language-js">fetch('https://jsonplaceholder.typicode.com/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))
// Output
{
id: 101,
title: 'foo',
body: 'bar',
userId: 1
}
</code></pre>
<p>Important: the resource will not be really created on the server but it will be faked as if. In other words, if you try to access a post using 101 as an id, you'll get a 404 error.</p>
<h3 id="updatearesource">Update a resource</h3>
<h4 id="withput">With PUT</h4>
<pre><code class="js language-js">fetch('https://jsonplaceholder.typicode.com/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))
// Output
{
id: 1,
title: 'foo',
body: 'bar',
userId: 1
}
</code></pre>
<h4 id="withpatch">With PATCH</h4>
<pre><code class="js language-js">fetch('https://jsonplaceholder.typicode.com/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))
// Output
{
id: 1,
title: 'foo',
body: '[...]',
userId: 1
}
</code></pre>
<p>Important: the resource will not be really updated on the server but it will be faked as if. </p>
<h3 id="deletearesource">Delete a resource</h3>
<pre><code class="js language-js">fetch('https://jsonplaceholder.typicode.com/posts/1', {
method: 'DELETE'
})
</code></pre>
<p>Important: the resource will not be really deleted on the server but it will be faked as if. </p>
<h3 id="filterresources">Filter resources</h3>
<p>Basic filtering is supported through query parameters.</p>
<pre><code class="js language-js">// Will return all the posts that belong to the first user
fetch('https://jsonplaceholder.typicode.com/posts?userId=1')
.then(response => response.json())
.then(json => console.log(json))
</code></pre>
<h3 id="nestedresources">Nested resources</h3>
<p>One level of nested route is available.</p>
<pre><code class="js language-js">// Equivalent to /comments?postId=1
fetch('https://jsonplaceholder.typicode.com/posts/1/comments')
.then(response => response.json())
.then(json => console.log(json))
</code></pre>
<p>Available nested routes:</p>
<ul>
<li><a href="https://jsonplaceholder.typicode.com/posts/1/comments">https://jsonplaceholder.typicode.com/posts/1/comments</a></li>
<li><a href="https://jsonplaceholder.typicode.com/albums/1/photos">https://jsonplaceholder.typicode.com/albums/1/photos</a></li>
<li><a href="https://jsonplaceholder.typicode.com/users/1/albums">https://jsonplaceholder.typicode.com/users/1/albums</a></li>
<li><a href="https://jsonplaceholder.typicode.com/users/1/todos">https://jsonplaceholder.typicode.com/users/1/todos</a></li>
<li><a href="https://jsonplaceholder.typicode.com/users/1/posts">https://jsonplaceholder.typicode.com/users/1/posts</a></li>
</ul>
<p></main></p>
</div>
<!-- Footer -->
<footer>
<div class="container">
<div style="margin-bottom: 2rem;">
<!-- <a href="https://www.patreon.com/bePatron?c=784328"> -->
<!-- <img -->
<!-- src="https://c5.patreon.com/external/logo/become_a_patron_button@2x.png" -->
<!-- width="217" -->
<!-- /> -->
<!-- </a> -->
<a href="https://github.com/users/typicode/sponsorship">
<i class="fab fa-github-alt"></i>
<strong>Sponsor this project on GitHub</strong>
</a>
</div>
<div>
Coded and built with ❤️ by
<a href="https://github.com/typicode">typicode</a>
<br />Source code and CHANGELOG available on
<a href="https://github.com/typicode/jsonplaceholder">GitHub</a>
</div>
</div>
</footer>
<!-- Analytics -->
<script>
;(function (i, s, o, g, r, a, m) {
i['GoogleAnalyticsObject'] = r
;(i[r] =
i[r] ||
function () {
;(i[r].q = i[r].q || []).push(arguments)
}),
(i[r].l = 1 * new Date())
;(a = s.createElement(o)), (m = s.getElementsByTagName(o)[0])
a.async = 1
a.src = g
m.parentNode.insertBefore(a, m)
})(
window,
document,
'script',
'//www.google-analytics.com/analytics.js',
'ga',
)
ga('create', 'UA-44497010-1', 'typicode.com')
ga('send', 'pageview')
var trackOutboundLink = function (url) {
ga('send', 'event', 'outbound', 'click', url, {
transport: 'beacon',
})
}
</script>
<script src="//cdnjs.cloudflare.com/ajax/libs/fetch/2.0.3/fetch.min.js"></script>
<script>
// Use http or https based on location.protocol
var example = document.getElementById('example')
example.textContent = example.textContent.replace(
'http:',
location.protocol,
)
// Highlight result element
var result = document.getElementById('result')
hljs.highlightBlock(result)
// Run example
var runButton = document.getElementById('run-button')
runButton.onclick = function () {
var root = location.protocol + '//jsonplaceholder.typicode.com'
var runMessage = document.getElementById('run-message')
// Hide or disable things during API call
runButton.disabled = true
runMessage.style.opacity = 0
result.style.opacity = 0
fetch('https://jsonplaceholder.typicode.com/todos/1')
.then((response) => response.json())
.then((json) => {
var str = JSON.stringify(json, null, '\t')
// Format result
result.innerHTML = str
.replace(/\n/g, '<br/>')
.replace(/\\n/g, ' ')
.replace(/\t/g, ' ')
hljs.highlightBlock(result)
// Show and enable things after API call
runButton.disabled = false
runMessage.style.opacity = 1
result.style.opacity = 1
})
}
</script>
</body>
</html>