Requestify
is a flexible and reusable network request utility built using Alamofire. It allows developers to easily construct and send HTTP requests with customizable methods, headers, parameters, and logging options.
- Simple, chainable interface to set URL, HTTP method, headers, and parameters.
- Supports request and response logging for easier debugging.
- Handles both JSON-encoded parameters and decodable responses.
- Utilizes Swift's async/await pattern for modern concurrency support.
- Supports multiple HTTP methods (GET, POST, PUT, DELETE, etc.).
- Allows for raw HTTP responses without decoding.
- Easily customizable for different API configurations.
- Supports multipart form data uploads, including images and JSON objects.
To create a request, use the RequestBuilder struct by chaining configuration methods:
import Requestify
let requestify = Requestify()
.setURL("https://jsonplaceholder.typicode.com/posts")
.setMethod(.get)
.setPrintLog(true)
Once your request is configured, send the request and handle the response using Swift's async/await:
struct Post: Codable {
let userId: Int
let id: Int
let title: String
let body: String
}
Task {
do {
let posts: [Post] = try await requestify.request([Post].self)
print("Posts: \(posts)")
} catch {
print("Error: \(error)")
}
}
You can send custom headers and parameters with your request:
let customHeaders: HTTPHeaders = [
"Authorization": "Bearer token"
]
let parameters = Params()
.add("userId", value: 1)
.build()
let requestify = Requestify()
.setURL("https://jsonplaceholder.typicode.com/posts")
.setMethod(.post)
.setHeaders(customHeaders)
.setParameters(parameters)
.setPrintResponse(false)
If you need a raw HTTPURLResponse without decoding the body, use:
Task {
do {
let response = try await requestify.requeust()
print("Response status code: \(response.statusCode)")
} catch {
print("Error: \(error)")
}
}
For uploading files or images, Requestify provides an easy interface for creating multipart requests:
import UIKit
let post = Post()
let requestify = Requestify()
.setURL("https://yourapi.com/upload")
.setMethod(.post)
.addObject(post, withName: "post")
.addImages([UIImage(named: "example")], withName: "file")
.setPrintResponse(false)
Task {
do {
let response: YourResponseType = try await requestify.upload(YourResponseType.self)
print("Upload successful: \(response)")
} catch {
print("Error: \(error)")
}
}
You can control the request and response logging using setPrintLog and setPrintResponse:
.setPrintLog(true)
.setPrintResponse(false)