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
Values in the query parameter dictionary must implement the `Queryable` protocol, `String` and `Array` types implement this by default.
96
99
97
-
### Body parameters
100
+
## Body parameters
101
+
102
+
### Simple body
98
103
99
104
```swift
100
105
testServerApi.post("/posts", data: ["body":"something","id":1, "title":"Some title", "userId":9]) { (error, object) in
101
106
//...
102
107
}
103
108
```
109
+
104
110
Body parameters should comform to `ValidJSONObject` protocol. `Array` and `Dictionary` types implement this by default.
105
111
Any custom type can implement `ValidJSONObject`, which requres a function that converts your type to `Data`.
106
112
107
113
```swift
108
114
funcJSONFormat() throws-> Data
109
115
```
110
116
117
+
### Custom object in body
118
+
111
119
If you don't want that much controll and resposibility, you can implement `JSONConvertible`, which has a property named `parameterValue` which can be any `ValidJSONObject`. Practically you just have to convert your type to a `Dictionary` or an other `ValidJSONObject` just it like this:
The framework also supports `form-encoded` requests as long as the response is in `JSON` format. To send one use a similar json request, but the body must comform to `ValidFormData` protocol.
150
+
151
+
If you want to save time, and ok with simple `[String: String]` format, just implement the `FormEncodable` protocol, which is much easyer and automatically conform to `ValidFormData`.
152
+
153
+
`Dictionary` has an added element called `formValue`, which is automatically conforms to `ValidFormData`. This is mostly a workaruond for a problem described [later](###Disclaimer)
oldServerApi.post("/post.php", query: ["dir":"gujci_test"], data: uploadData.formValue){ (error, response) in
166
+
// ... do something
167
+
}
168
+
```
169
+
170
+
## Authenticating requests
139
171
140
172
To authenticate a request, you have to set the `authentication` property of the API instance. For now this framework supports simple, access token based authentications in HTTP header and URL query. This is a simple example to show how to set up a HTTP header based authentication.
141
173
@@ -159,6 +191,23 @@ var sessionAuthenticator: RequestAuthenticator {
159
191
//...
160
192
```
161
193
194
+
## General body protocol
195
+
196
+
Any request body sent, must conform to `ValidRequestData` protocol. The 2 mentioned above are just some pre-implemented, widely used cases. If you use a custom protocol, or something, which is not implemented in this framework, just made the desired types conform to `ValidRequestData`, and they can be automatically used with the framework.
197
+
198
+
```swift
199
+
publicprotocolValidRequestData {
200
+
// defines the content type header
201
+
functype() -> ContentType
202
+
// returns the data, to sent to server
203
+
funcrequestData() throws-> Data
204
+
}
205
+
```
206
+
207
+
### Disclaimer
208
+
209
+
One type must not implement this protocol twice or more, meaning it is not supported to conform to both `ValidFormData` and `JSONConvertible`.
210
+
162
211
# Debugging
163
212
164
213
To turn on request logging add `APIRequestLoggingEnabled` to 'Arguments passed on launch' at Schema/Run/Arguments.
@@ -175,4 +224,4 @@ To log server sent errrors turn on `APIErrorLoggingEnabled`.
175
224
-[ ] make JSON and [JSON] comform to JSONParseable to reduce redundant code
176
225
-[ ] Add more unit tests
177
226
-[x] Travis
178
-
-[] Document form-encoded-support related changes
227
+
-[x] Document form-encoded-support related changes
0 commit comments