Skip to content

Commit 302ec59

Browse files
committed
Merge pull request #2165 from phalt/httpie-examples
Use httpie for tutorials
2 parents fd02d82 + f3ebac0 commit 302ec59

File tree

4 files changed

+132
-25
lines changed

4 files changed

+132
-25
lines changed

docs/tutorial/1-serialization.md

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -326,17 +326,51 @@ Quit out of the shell...
326326

327327
In another terminal window, we can test the server.
328328

329-
We can get a list of all of the snippets.
330-
331-
curl http://127.0.0.1:8000/snippets/
332-
333-
[{"id": 1, "title": "", "code": "foo = \"bar\"\n", "linenos": false, "language": "python", "style": "friendly"}, {"id": 2, "title": "", "code": "print \"hello, world\"\n", "linenos": false, "language": "python", "style": "friendly"}]
329+
We can test our API using using [curl][curl] or [httpie][httpie]. Httpie is a user friendly http client that's written in Python. Let's install that.
330+
331+
You can install httpie using pip:
332+
333+
pip install httpie
334+
335+
Finally, we can get a list of all of the snippets:
336+
337+
http http://127.0.0.1:8000/snippets/
338+
339+
HTTP/1.1 200 OK
340+
...
341+
[
342+
{
343+
"id": 1,
344+
"title": "",
345+
"code": "foo = \"bar\"\n",
346+
"linenos": false,
347+
"language": "python",
348+
"style": "friendly"
349+
},
350+
{
351+
"id": 2,
352+
"title": "",
353+
"code": "print \"hello, world\"\n",
354+
"linenos": false,
355+
"language": "python",
356+
"style": "friendly"
357+
}
358+
]
334359

335-
Or we can get a particular snippet by referencing its id.
360+
Or we can get a particular snippet by referencing its id:
336361

337-
curl http://127.0.0.1:8000/snippets/2/
362+
http http://127.0.0.1:8000/snippets/2/
338363

339-
{"id": 2, "title": "", "code": "print \"hello, world\"\n", "linenos": false, "language": "python", "style": "friendly"}
364+
HTTP/1.1 200 OK
365+
...
366+
{
367+
"id": 2,
368+
"title": "",
369+
"code": "print \"hello, world\"\n",
370+
"linenos": false,
371+
"language": "python",
372+
"style": "friendly"
373+
}
340374

341375
Similarly, you can have the same json displayed by visiting these URLs in a web browser.
342376

@@ -353,3 +387,5 @@ We'll see how we can start to improve things in [part 2 of the tutorial][tut-2].
353387
[sandbox]: http://restframework.herokuapp.com/
354388
[virtualenv]: http://www.virtualenv.org/en/latest/index.html
355389
[tut-2]: 2-requests-and-responses.md
390+
[httpie]: https://github.com/jakubroztocil/httpie#installation
391+
[curl]: http://curl.haxx.se

docs/tutorial/2-requests-and-responses.md

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -127,31 +127,64 @@ Go ahead and test the API from the command line, as we did in [tutorial part 1][
127127

128128
We can get a list of all of the snippets, as before.
129129

130-
curl http://127.0.0.1:8000/snippets/
131-
132-
[{"id": 1, "title": "", "code": "foo = \"bar\"\n", "linenos": false, "language": "python", "style": "friendly"}, {"id": 2, "title": "", "code": "print \"hello, world\"\n", "linenos": false, "language": "python", "style": "friendly"}]
130+
http http://127.0.0.1:8000/snippets/
131+
132+
HTTP/1.1 200 OK
133+
...
134+
[
135+
{
136+
"id": 1,
137+
"title": "",
138+
"code": "foo = \"bar\"\n",
139+
"linenos": false,
140+
"language": "python",
141+
"style": "friendly"
142+
},
143+
{
144+
"id": 2,
145+
"title": "",
146+
"code": "print \"hello, world\"\n",
147+
"linenos": false,
148+
"language": "python",
149+
"style": "friendly"
150+
}
151+
]
133152

134153
We can control the format of the response that we get back, either by using the `Accept` header:
135154

136-
curl http://127.0.0.1:8000/snippets/ -H 'Accept: application/json' # Request JSON
137-
curl http://127.0.0.1:8000/snippets/ -H 'Accept: text/html' # Request HTML
155+
http http://127.0.0.1:8000/snippets/ Accept:application/json # Request JSON
156+
http http://127.0.0.1:8000/snippets/ Accept:text/html # Request HTML
138157

139158
Or by appending a format suffix:
140159

141-
curl http://127.0.0.1:8000/snippets/.json # JSON suffix
142-
curl http://127.0.0.1:8000/snippets/.api # Browsable API suffix
160+
http http://127.0.0.1:8000/snippets/.json # JSON suffix
161+
http http://127.0.0.1:8000/snippets/.api # Browsable API suffix
143162

144163
Similarly, we can control the format of the request that we send, using the `Content-Type` header.
145164

146165
# POST using form data
147-
curl -X POST http://127.0.0.1:8000/snippets/ -d "code=print 123"
166+
http --form POST http://127.0.0.1:8000/snippets/ code="print 123"
148167

149-
{"id": 3, "title": "", "code": "print 123", "linenos": false, "language": "python", "style": "friendly"}
168+
{
169+
"id": 3,
170+
"title": "",
171+
"code": "print 123",
172+
"linenos": false,
173+
"language": "python",
174+
"style": "friendly"
175+
}
150176

151177
# POST using JSON
152-
curl -X POST http://127.0.0.1:8000/snippets/ -d '{"code": "print 456"}' -H "Content-Type: application/json"
153-
154-
{"id": 4, "title": "", "code": "print 456", "linenos": true, "language": "python", "style": "friendly"}
178+
http --json POST http://127.0.0.1:8000/snippets/ code="print 456"
179+
180+
{
181+
"id": 4,
182+
"title": "",
183+
"code": "print 456",
184+
"linenos": true,
185+
"language": "python",
186+
"style": "friendly"
187+
}
155188

156189
Now go and open the API in a web browser, by visiting [http://127.0.0.1:8000/snippets/][devserver].
157190

docs/tutorial/4-authentication-and-permissions.md

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,15 +198,25 @@ If we're interacting with the API programmatically we need to explicitly provide
198198

199199
If we try to create a snippet without authenticating, we'll get an error:
200200

201-
curl -i -X POST http://127.0.0.1:8000/snippets/ -d "code=print 123"
201+
http POST http://127.0.0.1:8000/snippets/ code="print 123"
202202

203-
{"detail": "Authentication credentials were not provided."}
203+
{
204+
"detail": "Authentication credentials were not provided."
205+
}
204206

205207
We can make a successful request by including the username and password of one of the users we created earlier.
206208

207-
curl -X POST http://127.0.0.1:8000/snippets/ -d "code=print 789" -u tom:password
208-
209-
{"id": 5, "owner": "tom", "title": "foo", "code": "print 789", "linenos": false, "language": "python", "style": "friendly"}
209+
http POST -a tom:password http://127.0.0.1:8000/snippets/ code="print 789"
210+
211+
{
212+
"id": 5,
213+
"owner": "tom",
214+
"title": "foo",
215+
"code": "print 789",
216+
"linenos": false,
217+
"language": "python",
218+
"style": "friendly"
219+
}
210220

211221
## Summary
212222

docs/tutorial/quickstart.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,33 @@ We can now access our API, both from the command-line, using tools like `curl`..
158158
]
159159
}
160160

161+
Or using the [httpie][httpie], command line tool...
162+
163+
bash: http -a username:password http://127.0.0.1:8000/users/
164+
165+
HTTP/1.1 200 OK
166+
...
167+
{
168+
"count": 2,
169+
"next": null,
170+
"previous": null,
171+
"results": [
172+
{
173+
"email": "[email protected]",
174+
"groups": [],
175+
"url": "http://localhost:8000/users/1/",
176+
"username": "paul"
177+
},
178+
{
179+
"email": "[email protected]",
180+
"groups": [ ],
181+
"url": "http://127.0.0.1:8000/users/2/",
182+
"username": "tom"
183+
}
184+
]
185+
}
186+
187+
161188
Or directly through the browser...
162189

163190
![Quick start image][image]
@@ -172,3 +199,4 @@ If you want to get a more in depth understanding of how REST framework fits toge
172199
[image]: ../img/quickstart.png
173200
[tutorial]: 1-serialization.md
174201
[guide]: ../#api-guide
202+
[httpie]: https://github.com/jakubroztocil/httpie#installation

0 commit comments

Comments
 (0)