@@ -7,8 +7,12 @@ A small example of how an API could be written in Symfony. This project allow a
7
7
8
8
All output is standardized so that it is easy to parse in any language on any environment.
9
9
10
+ This branch features an implementation of [ JSON Web Tokens] ( https://jwt.io/ ) for authenticating users.
11
+
10
12
## Features
11
13
14
+ - JWT Authentication
15
+ - [ GET token] ( #get-a-token )
12
16
- Data retrieval
13
17
- [ GET all entities] ( #get-all-entities )
14
18
- [ GET entity by id] ( #get-entity-by-id )
@@ -20,20 +24,23 @@ All output is standardized so that it is easy to parse in any language on any en
20
24
21
25
## URLS
22
26
23
- ```
24
- // GET
27
+ ``` bash
28
+ # POST retrieve a token
29
+ symfony.app/api/token
30
+
31
+ # GET car
25
32
symfony.app/car
26
33
27
- // GET by id
34
+ # GET car by id
28
35
symfony.app/car/{id}
29
36
30
- // POST new
37
+ # POST new car
31
38
symfony.app/car
32
39
33
- // PATCH update
40
+ # PATCH update car
34
41
symfony.app/car/{id}
35
42
36
- // DELETE remove
43
+ # DELETE remove car
37
44
symfony.app/car/{id}
38
45
```
39
46
@@ -55,19 +62,57 @@ symfony.app/car/{id}
55
62
### Testing
56
63
57
64
1 . Run ` php bin/console doc:mig:mig --env=test `
58
- 2 . Run ` composer test `
65
+ 2 . Run ` php bin/console doc:fixtures:load --env=test `
66
+ 3 . Run ` composer test `
59
67
60
- For more information which tests are run, please refer to the ` "test" ` section of ` composer.json `
68
+ For more information which tests are run, please refer to the ` "test" ` section of ` composer.json ` .
69
+ You can run the given tests separately, i.e. ` composer behat ` or ` composer phpunit `
61
70
62
71
## Technical Docs
63
72
73
+ ### Error messages
74
+
75
+ All error messages have the same format, so they can be easily parsed in any language:
76
+
77
+ ``` JSON
78
+ {
79
+ "error" : {
80
+ "code" : 401 ,
81
+ "message" : " Not privileged to request the resource."
82
+ }
83
+ }
84
+ ```
85
+
86
+ ### GET a token
87
+
88
+ #### Request
89
+ ``` bash
90
+ $ curl --request POST \
91
+ --url http://symfony.app/api/token \
92
+ --header ' password: unsafepassword' \
93
+ --header ' username: admin'
94
+ ```
95
+
96
+ #### Response
97
+ ``` JSON
98
+ {
99
+ "token" : " eyJhbGciOiJSUzI1NiJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwiZXhwIjoxNDg0NTgyNTc4LCJpYXQiOjE0ODQ1ODE5Nzh9.t31C2jYVHWybZ2szEFwkGEzspYFyg9BlTyolnYtznnm8eFPIZI00hZPYCPFX2Ka7-gBFb3keM_2WVhfXKvreQpaFzge2HQ1lfMgVBCCUsxoiESUo6qCkna0Vb6ttv1qLyBRAqui_ijjANaAqEgO648vnIP0BMOYkjzw9-jNJNRQ25Bv4Y7bc_LGcGJQc2wGlg5sxWqMYhHwwCncBNPpdwTj9e9WULGBv0U1Hc_8I5eCrQFrCJGeQaKnEiy1GKXdRCSqwfCqEDrbXhgkBGygUbPGAYrfU8SnrtxFRI_EN92PByo2rjpy_M5gL-Md6czN5xDSxJHmswValR-I1ga1WkqEf194erD7KJmRRXUpz1HwNDWPDm1RJfzVgj0vTlW7kCKdLqGkkvaVnPuToxLhAPnp-kfdFkprtND0J8CajdiKaYVia4DwOjK4w_lbnfLMzZp6s6o7eKQ4h7_vkZAGu_DA0f6fVOuGQc5cqef_1oMqbKKrhVWL4xMg9wovpkAm_AF-iii-cjaXejArKzZ_4sKku5fc7BleSIHH0sXXLWlE_bI6ftc3AAxTl1buIOwpqrKDwlU_YfO8d9YkuZCRG-I0B8Nu0hfW6qh3jwIaqlqaAP6ZqAfAk8Sd6cQw8eqSqjhFjtSKA2J-DYn4lP2DC-0-_6ydj8sl3pB-DV7MEVVI"
100
+ }
101
+ ```
102
+
64
103
### GET all entities
65
- ``` shell
66
- # Request
104
+
105
+ #### Request
106
+
107
+ ``` bash
67
108
$ curl --request GET \
68
- --url http://symfony.app/car
69
-
70
- # Response
109
+ --url http://symfony.app/car \
110
+ --header ' Authorization: Bearer {token}'
111
+ ```
112
+
113
+ #### Response
114
+
115
+ ``` JSON
71
116
[
72
117
{
73
118
"id" : 1 ,
@@ -86,12 +131,17 @@ $ curl --request GET \
86
131
```
87
132
88
133
### GET entity by id
89
- ``` shell
90
- # Request
134
+
135
+ #### Request
136
+
137
+ ``` bash
91
138
$ curl --request GET \
92
- --url http://symfony.app/car/1
93
-
94
- # Response
139
+ --url http://symfony.app/car/{id} \
140
+ --header ' Authorization: Bearer {token}'
141
+ ```
142
+
143
+ #### Response
144
+ ``` JSON
95
145
{
96
146
"id" : 1 ,
97
147
"brand" : " Ford" ,
@@ -101,18 +151,23 @@ $ curl --request GET \
101
151
```
102
152
103
153
### POST new entity
104
- ``` shell
105
- # Request
154
+
155
+ #### Request
156
+
157
+ ``` bash
106
158
$ curl --request POST \
107
159
--url http://symfony.app/car \
108
160
--header ' content-type: application/json' \
161
+ --header ' Authorization: Bearer {token}'
109
162
--data ' {
110
163
"brand": "Ford",
111
164
"name": "Mustang",
112
165
"year": 1972
113
166
}'
114
-
115
- # Response
167
+ ```
168
+
169
+ #### Response
170
+ ``` JSON
116
171
{
117
172
"id" : 1 ,
118
173
"brand" : " Ford" ,
@@ -122,16 +177,20 @@ $ curl --request POST \
122
177
```
123
178
124
179
### PATCH update existing entity
125
- ``` shell
126
- # Request
180
+
181
+ #### Request
182
+ ``` bash
127
183
$ curl --request PATCH \
128
- --url http://symfony.app/car/1 \
184
+ --url http://symfony.app/car/{id} \
129
185
--header ' content-type: application/json' \
186
+ --header ' Authorization: Bearer {token} \
130
187
--data ' {
131
188
" year" : 2016
132
189
}'
133
-
134
- # Response
190
+ ```
191
+
192
+ #### Response
193
+ ```JSON
135
194
{
136
195
"id": 1,
137
196
"brand": "Ford",
@@ -141,12 +200,16 @@ $ curl --request PATCH \
141
200
```
142
201
143
202
### DELETE remove existing entity
144
- ``` shell
145
- # Request
203
+
204
+ #### Request
205
+ ```bash
146
206
$ curl --request DELETE \
147
- --url http://symfony.app/car/1
207
+ --url http://symfony.app/car/{id} \
208
+ --header ' Authorization: Bearer {token}
209
+ ```
148
210
149
- # Response
211
+ #### Response
212
+ ``` JSON
150
213
{
151
214
"message" : " Car deleted"
152
215
}
0 commit comments