@@ -676,15 +676,47 @@ Development server is running at http://127.0.0.1:8000/
...
@@ -676,15 +676,47 @@ Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Quit the server with CONTROL-C.
</code></pre>
</code></pre>
<p>In another terminal window, we can test the server.</p>
<p>In another terminal window, we can test the server.</p>
<p>We can get a list of all of the snippets.</p>
<p>We can test our API using using <ahref="http://curl.haxx.se">curl</a> or <ahref="https://github.com/jakubroztocil/httpie#installation">httpie</a>. Httpie is a user friendly http client that's written in Python. Let's install that.</p>
<p>Go ahead and test the API from the command line, as we did in <ahref="../1-serialization">tutorial part 1</a>. Everything is working pretty similarly, although we've got some nicer error handling if we send invalid requests.</p>
<p>Go ahead and test the API from the command line, as we did in <ahref="../1-serialization">tutorial part 1</a>. Everything is working pretty similarly, although we've got some nicer error handling if we send invalid requests.</p>
<p>We can get a list of all of the snippets, as before.</p>
<p>We can get a list of all of the snippets, as before.</p>
@@ -548,14 +548,24 @@ class IsOwnerOrReadOnly(permissions.BasePermission):
...
@@ -548,14 +548,24 @@ class IsOwnerOrReadOnly(permissions.BasePermission):
<p>When we interact with the API through the web browser, we can login, and the browser session will then provide the required authentication for the requests.</p>
<p>When we interact with the API through the web browser, we can login, and the browser session will then provide the required authentication for the requests.</p>
<p>If we're interacting with the API programmatically we need to explicitly provide the authentication credentials on each request.</p>
<p>If we're interacting with the API programmatically we need to explicitly provide the authentication credentials on each request.</p>
<p>If we try to create a snippet without authenticating, we'll get an error:</p>
<p>If we try to create a snippet without authenticating, we'll get an error:</p>
<pre><code>curl -i -X POST http://127.0.0.1:8000/snippets/ -d "code=print 123"
<pre><code>http POST http://127.0.0.1:8000/snippets/ code="print 123"
{"detail": "Authentication credentials were not provided."}
{
"detail": "Authentication credentials were not provided."
}
</code></pre>
</code></pre>
<p>We can make a successful request by including the username and password of one of the users we created earlier.</p>
<p>We can make a successful request by including the username and password of one of the users we created earlier.</p>
<pre><code>curl -X POST http://127.0.0.1:8000/snippets/ -d "code=print 789" -u tom:password
<pre><code>http POST -a tom:password http://127.0.0.1:8000/snippets/ code="print 789"
<p>We've now got a fairly fine-grained set of permissions on our Web API, and end points for users of the system and for the code snippets that they have created.</p>
<p>We've now got a fairly fine-grained set of permissions on our Web API, and end points for users of the system and for the code snippets that they have created.</p>