You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<p>Django REST framework is used to create REST API</p>
18
+
19
+
<p>Following API endpoints are created</p>
20
+
21
+
<ul>
22
+
<li>Login endpoint - POST</li>
23
+
<li>Logout endpoint - POST</li>
24
+
<li>Posts endpoint - GET/POST</li>
25
+
<li>Comments endpoint - GET/POST</li>
26
+
</ul>
27
+
28
+
<p><i><b>Note : </b>All api requests example shown here are using httpie library</i></p>
29
+
30
+
<h3>Login endpoint - POST </h3>
31
+
<p>This is used to login a user. We need to pass username, password. In return we get a auth token which we need to pass along with all further subsequent post requests.</p>
32
+
<p>You can pass values as form-data or json.</p>
33
+
<p><code>http --form post https://mir1198yusuf1.pythonanywhere.com/api/login/ username=username password=password</code></p>
34
+
<p><code>http --json post https://mir1198yusuf1.pythonanywhere.com/api/login/ username=username password=password</code></p>
35
+
<p>The response data for correct username-password will be : </p>
<p>For empty/invalid username-password response would be : </p>
38
+
<p><code>{"password":["This field may not be blank."],"username":["This field may not be blank."]}</code></p>
39
+
<p><code>{"non_field_errors":["Unable to log in with provided credentials."]}</code></p>
40
+
41
+
<h3>Logout endpoint - POST </h3>
42
+
<p>This is used to logout a user. We need to pass auth token received from login endpoint as Authorization header. Once user is logout, the auth token is removed from database & considered as invalid for further post request.</p>
43
+
<p>You can pass values as form-data or json.</p>
44
+
<p><code>http --form post https://mir1198yusuf1.pythonanywhere.com/api/logout/ 'Authorization: Token xxxxxxxxxxx'</code></p>
45
+
<p><code>http --json post https://mir1198yusuf1.pythonanywhere.com/api/logout/ 'Authorization: Token xxxxxxxxxxx'</code></p>
46
+
<p>No response data will be returned for correct auth token and user will be logged out.</p>
47
+
<p>For empty/invalid token-header response would be : </p>
48
+
<p><code>{"detail":"Authentication credentials were not provided."}</code></p>
49
+
<p><code>{"detail":"Invalid token."}</code></p>
50
+
51
+
<h3>Posts endpoint - GET </h3>
52
+
<p>This is used to get the list of all posts.</p>
53
+
<p><code>http get https://mir1198yusuf1.pythonanywhere.com/api/posts/</code></p>
54
+
<p>The response data will be like :</p>
55
+
<p><code>[{"created_by":{"email":"[email protected]","id":1,"username":"aaa"},"created_on":"2020-06-18T12:57:40.470161+05:30","created_on_humanized":"2 weeks ago","file":"/media/download.jpeg","id":2,"message":"second post with image"},{"created_by":{"email":"[email protected]","id":1,"username":"aaa"},"created_on":"2020-06-18T12:57:13.274334+05:30","created_on_humanized":"2 weeks ago","file":null,"id":1,"message":"first post"}]</code></p>
56
+
57
+
<h3>Posts endpoint - POST </h3>
58
+
<p>This is used to create a new post. We have to pass auth token in header.</p>
59
+
<p>Since it contains a file field, we have to send data in form-data only. no json.</p>
60
+
<p>File field is optional but message is mandatory</p>
61
+
<p><code>http --form post https://mir1198yusuf1.pythonanywhere.com/api/posts/ message="this is the post message" [email protected] 'Authorization: Token xxxxxxxx'</code></p>
62
+
<p>The response data will be created post :</p>
63
+
<p><code>{"created_by":{"email":"[email protected]","id":2,"username":"aaa"},"created_on":"2020-07-02T15:15:00.948653+05:30","created_on_humanized":"now","file":"/media/runthis.txt","id":9,"message":"this is the post message"}</code></p>
64
+
<p>For empty/invalid values, response can be like :</p>
65
+
<p><code>{"detail":"Authentication credentials were not provided."}</code></p>
66
+
<p><code>{"message":["This field is required."]}</code></p>
67
+
68
+
<h3>Comments endpoint - GET </h3>
69
+
<p>This is used to get list of all comments for a post. We have to pass a valid post id in url.</p>
70
+
<p><code>http get https://mir1198yusuf1.pythonanywhere.com/api/posts/2/</code></p>
71
+
<p>The response data will be like :</p>
72
+
<p><code>[{"created_by":{"email":"[email protected]","id":1,"username":"aaa"},"created_on":"2020-06-18T13:02:09.492529+05:30","created_on_humanized":"2 weeks ago","id":1,"message":"first comment","post":{"created_by":{"email":"[email protected]","id":1,"username":"aaa"},"created_on":"2020-06-18T12:57:40.470161+05:30","created_on_humanized":"2 weeks ago","file":"/media/download.jpeg","id":2,"message":"second post with image"}}]</code></p>
73
+
74
+
<h3>Comments endpoint - POST </h3>
75
+
<p>This is used to create a new comment for a given post. We have to pass valid post id in url and auth token in header.</p>
76
+
<p>We can pass data in both form-data and json format.</p>
77
+
<p><code>http --json post https://mir1198yusuf1.pythonanywhere.com/api/posts/1/ message="second comment" 'Authorization: Token xxxxxx'</code></p>
78
+
<p><code>http --form post https://mir1198yusuf1.pythonanywhere.com/api/posts/1/ message="second comment" 'Authorization: Token xxxxxx'</code></p>
79
+
<p>The response data will be created comment :</p>
0 commit comments