aboutsummaryrefslogtreecommitdiffstats
path: root/docs/tutorial/2-requests-and-responses.md
diff options
context:
space:
mode:
authorTom Christie2013-02-23 21:29:52 +0000
committerTom Christie2013-02-23 21:29:52 +0000
commit5b03de8287b04177c30815f5c798fe5c69e0d122 (patch)
treeea9a6ad38cfbe94c81ea1ee320195fce7d78da9b /docs/tutorial/2-requests-and-responses.md
parent41d3fe09cf3dd80cb8efed13dd886645ae7be470 (diff)
downloaddjango-rest-framework-5b03de8287b04177c30815f5c798fe5c69e0d122.tar.bz2
Add curl examples.
Diffstat (limited to 'docs/tutorial/2-requests-and-responses.md')
-rw-r--r--docs/tutorial/2-requests-and-responses.md36
1 files changed, 32 insertions, 4 deletions
diff --git a/docs/tutorial/2-requests-and-responses.md b/docs/tutorial/2-requests-and-responses.md
index 566c0dc6..63cee3a6 100644
--- a/docs/tutorial/2-requests-and-responses.md
+++ b/docs/tutorial/2-requests-and-responses.md
@@ -75,11 +75,11 @@ Here is the view for an individual snippet.
snippet = Snippet.objects.get(pk=pk)
except Snippet.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)
-
+
if request.method == 'GET':
serializer = SnippetSerializer(snippet)
return Response(serializer.data)
-
+
elif request.method == 'PUT':
serializer = SnippetSerializer(snippet, data=request.DATA)
if serializer.is_valid():
@@ -126,13 +126,41 @@ We don't necessarily need to add these extra url patterns in, but it gives us a
Go ahead and test the API from the command line, as we did in [tutorial part 1][tut-1]. Everything is working pretty similarly, although we've got some nicer error handling if we send invalid requests.
-**TODO: Describe using accept headers, content-type headers, and format suffixed URLs**
+We can get a list of all of the snippets, as before.
+
+ curl http://127.0.0.1:8000/snippets/
+
+ [{"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"}]
+
+We can control the format of the response that we get back, either by using the `Accept` header:
+
+ curl http://127.0.0.1:8000/snippets/ -H 'Accept: application/json' # Request JSON
+ curl http://127.0.0.1:8000/snippets/ -H 'Accept: text/html' # Request HTML
+
+Or by appending a format suffix:
+
+ curl http://127.0.0.1:8000/snippets/.json # JSON suffix
+ curl http://127.0.0.1:8000/snippets/.api # Browseable API suffix
+
+Similarly, we can control the format of the request that we send, using the `Content-Type` header.
+
+ # POST using form data
+ curl -X POST http://127.0.0.1:8000/snippets/ -d "code=print 123"
+
+ {"id": 3, "title": "", "code": "123", "linenos": false, "language": "python", "style": "friendly"}
+
+ # POST using JSON
+ curl -X POST http://127.0.0.1:8000/snippets/ -d '{"code": "print 456"}' -H "Content-Type: application/json"
+
+ {"id": 4, "title": "", "code": "print 456", "linenos": true, "language": "python", "style": "friendly"}
Now go and open the API in a web browser, by visiting [http://127.0.0.1:8000/snippets/][devserver].
### Browsability
-Because the API chooses a return format based on what the client asks for, it will, by default, return an HTML-formatted representation of the resource when that resource is requested by a browser. This allows for the API to be easily browsable and usable by humans.
+Because the API chooses the content type of the response based on the client request, it will, by default, return an HTML-formatted representation of the resource when that resource is requested by a web browser. This allows for the API to return a fully web-browsable HTML representation.
+
+Having a web-browseable API is a huge usability win, and makes developing and using your API much easier. It also dramatically lowers the barrier-to-entry for other developers wanting to inspect and work with your API.
See the [browsable api][browseable-api] topic for more information about the browsable API feature and how to customize it.