Added basic usage example to README.

This commit is contained in:
Tamás Kálcza 2022-03-21 13:54:13 +01:00
parent 530da47f0b
commit c9e3967d36
1 changed files with 21 additions and 0 deletions

View File

@ -51,3 +51,24 @@ media type.
MediaType.application(.other("vnd.efi.img")) // is equivalent to
let mediaType: MediaType = "application/vnd.efi.img"
```
## Using Media Types
### String Conversion
Since ``MediaType`` conforms to
the [`CustomStringConvertible`](https://developer.apple.com/documentation/swift/customstringconvertible) protocol it is
pretty straightforward to turn an instance into a string.
You can either call the ``MediaType/description`` computed property or simply embed an instance into an interpolated
string.
For example, you can request the list of available products in JSON from an imaginary store.
```swift
var request = URLRequest(url: URL(string: "https://example-store.com/products")!)
let contentType = MediaType.application(.json())
request.setValue("Content-Type", forHTTPHeaderField: contentType.description)
request.setValue("Content-Type", forHTTPHeaderField: "\(contentType)")
let (_, response) = try await URLSession.shared.data(for: request)
```