diff --git a/Examples/DeleteJSON/DeleteJSON.swift b/Examples/DeleteJSON/DeleteJSON.swift new file mode 100644 index 000000000..1b92b4ba5 --- /dev/null +++ b/Examples/DeleteJSON/DeleteJSON.swift @@ -0,0 +1,21 @@ +import AsyncHTTPClient +import Foundation +import NIOCore +import NIOFoundationCompat + +@main +struct DeleteJSON { + static func main() async throws { + let httpClient = HTTPClient.shared + + do { + var request = HTTPClientRequest(url: "https://jsonplaceholder.typicode.com/todos/1") + request.method = .DELETE + + let response = try await httpClient.execute(request, timeout: .seconds(30)) + print("HTTP head", response) + } catch { + print("request failed:", error) + } + } +} diff --git a/Examples/Package.swift b/Examples/Package.swift index 9986b17b5..7c7a3a937 100644 --- a/Examples/Package.swift +++ b/Examples/Package.swift @@ -26,6 +26,9 @@ let package = Package( products: [ .executable(name: "GetHTML", targets: ["GetHTML"]), .executable(name: "GetJSON", targets: ["GetJSON"]), + .executable(name: "PostJSON", targets: ["PostJSON"]), + .executable(name: "PutJSON", targets: ["PutJSON"]), + .executable(name: "DeleteJSON", targets: ["DeleteJSON"]), .executable(name: "StreamingByteCounter", targets: ["StreamingByteCounter"]), ], dependencies: [ @@ -55,6 +58,33 @@ let package = Package( ], path: "GetJSON" ), + .executableTarget( + name: "PostJSON", + dependencies: [ + .product(name: "AsyncHTTPClient", package: "async-http-client"), + .product(name: "NIOCore", package: "swift-nio"), + .product(name: "NIOFoundationCompat", package: "swift-nio"), + ], + path: "PostJSON" + ), + .executableTarget( + name: "PutJSON", + dependencies: [ + .product(name: "AsyncHTTPClient", package: "async-http-client"), + .product(name: "NIOCore", package: "swift-nio"), + .product(name: "NIOFoundationCompat", package: "swift-nio"), + ], + path: "PutJSON" + ), + .executableTarget( + name: "DeleteJSON", + dependencies: [ + .product(name: "AsyncHTTPClient", package: "async-http-client"), + .product(name: "NIOCore", package: "swift-nio"), + .product(name: "NIOFoundationCompat", package: "swift-nio"), + ], + path: "DeleteJSON" + ), .executableTarget( name: "StreamingByteCounter", dependencies: [ diff --git a/Examples/PostJSON/PostJSON.swift b/Examples/PostJSON/PostJSON.swift new file mode 100644 index 000000000..3fc652222 --- /dev/null +++ b/Examples/PostJSON/PostJSON.swift @@ -0,0 +1,35 @@ +import AsyncHTTPClient +import Foundation +import NIOCore +import NIOFoundationCompat + +struct Todo: Codable { + var id: Int? + var userId: Int + var title: String + var completed: Bool +} + +@main +struct PostJSON { + static func main() async throws { + let httpClient = HTTPClient(eventLoopGroupProvider: .singleton) + let payload = Todo(userId: 1, title: "Test Todo", completed: false) + + do { + let jsonData = try JSONEncoder().encode(payload) + + var request = HTTPClientRequest(url: "https://jsonplaceholder.typicode.com/todos") + request.method = .POST + request.headers.add(name: "Content-Type", value: "application/json") + request.body = .bytes(jsonData) + + let response = try await httpClient.execute(request, timeout: .seconds(30)) + print("HTTP head", response) + } catch { + print("request failed:", error) + } + // it is important to shutdown the httpClient after all requests are done, even if one failed + try await httpClient.shutdown() + } +} diff --git a/Examples/PutJSON/PutJSON.swift b/Examples/PutJSON/PutJSON.swift new file mode 100644 index 000000000..86234d5c2 --- /dev/null +++ b/Examples/PutJSON/PutJSON.swift @@ -0,0 +1,35 @@ +import AsyncHTTPClient +import Foundation +import NIOCore +import NIOFoundationCompat + +struct Todo: Codable { + var id: Int + var userId: Int + var title: String + var completed: Bool +} + +@main +struct PostJSON { + static func main() async throws { + let httpClient = HTTPClient(eventLoopGroupProvider: .singleton) + let payload = Todo(id: 1, userId: 1, title: "Test Todo", completed: false) + + do { + let jsonData = try JSONEncoder().encode(payload) + + var request = HTTPClientRequest(url: "https://jsonplaceholder.typicode.com/todos/\(payload.id)") + request.method = .PUT + request.headers.add(name: "Content-Type", value: "application/json") + request.body = .bytes(jsonData) + + let response = try await httpClient.execute(request, timeout: .seconds(30)) + print("HTTP head", response) + } catch { + print("request failed:", error) + } + // it is important to shutdown the httpClient after all requests are done, even if one failed + try await httpClient.shutdown() + } +}