Skip to content

Commit 348abd2

Browse files
committed
- RESTAPIImage added with multipart form data support
1 parent f11c3dc commit 348abd2

File tree

9 files changed

+637
-1
lines changed

9 files changed

+637
-1
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ The framework supports `GET`, `POST`, `PUT` and `DELETE` requests for now.
1515
github "Gujci/RESTAPI"
1616
```
1717

18+
For [Image upload](Image-upload) link `RESTAPIImage` as well.
19+
1820
This framework highly relies on [SwiftyJSON] (https://github.com/SwiftyJSON/SwiftyJSON), so it imports it.
1921

2022
### Swift 2.2
@@ -167,6 +169,27 @@ oldServerApi.post("/post.php", query: ["dir": "gujci_test"], data: uploadData.fo
167169
}
168170
```
169171

172+
### Multipart form request
173+
174+
`MultipartFormData` is a protocol, which provides default implementation for `ValidRequestData`. Implement this protocol to prepare any custom type to be uploaded as multipart form data.
175+
176+
## Image upload
177+
178+
As an optional extension `RESTAPIImage` adds a util implementation for `MultipartFormData` to upload a simple image. To perform an upload, just instantiate a new `JPGUploadMultipartFormData` instance with `UIImage` and send it as any other request.
179+
180+
`RESTAPIImage` is a sepatate framework, in order to use, add it to Carthage `copy-frameworks` phase and link it.
181+
182+
````swift
183+
let uploadData = JPGUploadMultipartFormData(image: image, fileName: "image", uploadName: "upfile")
184+
api.post("/me/profile_picture", data: uploadData) { (err, resp) in
185+
//...
186+
}
187+
````
188+
189+
As all methods requre a `ValidRequestData` to send in body, uploading `JPGUploadMultipartFormData` is not different from any json upload besides composing the `httpBody` of the request. Any utils like authentication can be used, the response parsing is the same.
190+
191+
If the `JPGUploadMultipartFormData` implemetation is not suited for the current usecase, custom implemetnation for `MultipartFormData` can be implemented easily, like described [before](###Multipart-form-request)
192+
170193
## Authenticating requests
171194

172195
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.

0 commit comments

Comments
 (0)