Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
s4cha authored Nov 16, 2023
1 parent f36d808 commit e8ac712
Showing 1 changed file with 35 additions and 8 deletions.
43 changes: 35 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,30 +114,57 @@ let postsPublisher: AnyPublisher<[Post], Error> = client.get("")
```

### Pass params
Simply pass a `[String: CustomStringConvertible]` dictionary to the `params` parameter.

#### GET params
GET params are query params, you can put them directly in the URL like so:

```swift
try await client.get("/posts?limit=20&offset=60")
```
or use the `params` parameter like so:

```swift
let response: Data = try await client.posts("/posts/1", params: ["optin" : true ])
try await client.get("/posts", params: ["limit" : 20, "offset" : 60])
```

Parameters are `.urlEncoded` by default (`Content-Type: application/x-www-form-urlencoded`), to encode them as json
(`Content-Type: application/json`), you need to set the client's `parameterEncoding` to `.json` as follows:
Both are equivalent.

#### POST, PUT, PATCH body
Parameters to POST, PUT & PATCH requests are passed in the http body.
You can specify them using the `body` parameter.

##### URLEncoded
`Content-Type: application/x-www-form-urlencoded`

For url encoded boy, use `HttpBody.urlEncoded` type for the `body` parameter with a `[String: CustomStringConvertible]` dictionary.
```swift
client.parameterEncoding = .json
try await client.post("/posts/1", body: .urlEncoded(["liked" : true ]))
```

##### JSON
`Content-Type: application/json`
For JSON encoded body, use `HttpBody.json` type for the `body` parameter with an `Encodable` object.

```swift
try await client.post("/posts/1", body: .json(["liked" : true ]))

or
try await client.post("/posts/1", body: .json(PostBody(liked: true))
// Where `PostBody` is Encodable`
```

### Upload multipart data
For multipart calls (post/put), just pass a `MultipartData` struct to the `multipartData` parameter.
For multipart calls (post/put), just pass a `HttpBody.multipart` type to the `body` parameter.
```swift
let params: [String: CustomStringConvertible] = [ "type_resource_id": 1, "title": photo.title]
let multipartData = MultipartData(name: "file",
fileData: photo.data,
fileName: "photo.jpg",
mimeType: "image/jpeg")
client.post("/photos/upload",
params: params,
multipartData: multipartData).sink(receiveCompletion: { _ in }) { (data:Data?, progress: Progress) in
body: .multipart(params: params,
parts: [multipartData]))
.sink(receiveCompletion: { _ in }) { (data:Data?, progress: Progress) in
if let data = data {
print("upload is complete : \(data)")
} else {
Expand Down

0 comments on commit e8ac712

Please sign in to comment.