Skip to content

Commit b00c506

Browse files
committed
feat: add http examples for POST PUT DELETE
1 parent 0b6f957 commit b00c506

File tree

4 files changed

+121
-0
lines changed

4 files changed

+121
-0
lines changed

Examples/DeleteJSON/DeleteJSON.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import AsyncHTTPClient
2+
import Foundation
3+
import NIOCore
4+
import NIOFoundationCompat
5+
6+
@main
7+
struct PostJSON {
8+
static func main() async throws {
9+
let httpClient = HTTPClient(eventLoopGroupProvider: .singleton)
10+
11+
do {
12+
var request = HTTPClientRequest(url: "http://localhost:8080/todos/1)")
13+
request.method = .DELETE
14+
15+
let response = try await httpClient.execute(request, timeout: .seconds(30))
16+
print("HTTP head", response)
17+
} catch {
18+
print("request failed:", error)
19+
}
20+
// it is important to shutdown the httpClient after all requests are done, even if one failed
21+
try await httpClient.shutdown()
22+
}
23+
}

Examples/Package.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ let package = Package(
2626
products: [
2727
.executable(name: "GetHTML", targets: ["GetHTML"]),
2828
.executable(name: "GetJSON", targets: ["GetJSON"]),
29+
.executable(name: "PostJSON", targets: ["PostJSON"]),
30+
.executable(name: "PutJSON", targets: ["PutJSON"]),
31+
.executable(name: "DeleteJSON", targets: ["DeleteJSON"]),
2932
.executable(name: "StreamingByteCounter", targets: ["StreamingByteCounter"]),
3033
],
3134
dependencies: [
@@ -55,6 +58,33 @@ let package = Package(
5558
],
5659
path: "GetJSON"
5760
),
61+
.executableTarget(
62+
name: "PostJSON",
63+
dependencies: [
64+
.product(name: "AsyncHTTPClient", package: "async-http-client"),
65+
.product(name: "NIOCore", package: "swift-nio"),
66+
.product(name: "NIOFoundationCompat", package: "swift-nio"),
67+
],
68+
path: "PostJSON"
69+
),
70+
.executableTarget(
71+
name: "PutJSON",
72+
dependencies: [
73+
.product(name: "AsyncHTTPClient", package: "async-http-client"),
74+
.product(name: "NIOCore", package: "swift-nio"),
75+
.product(name: "NIOFoundationCompat", package: "swift-nio"),
76+
],
77+
path: "PutJSON"
78+
),
79+
.executableTarget(
80+
name: "DeleteJSON",
81+
dependencies: [
82+
.product(name: "AsyncHTTPClient", package: "async-http-client"),
83+
.product(name: "NIOCore", package: "swift-nio"),
84+
.product(name: "NIOFoundationCompat", package: "swift-nio"),
85+
],
86+
path: "DeleteJSON"
87+
),
5888
.executableTarget(
5989
name: "StreamingByteCounter",
6090
dependencies: [

Examples/PostJSON/PostJSON.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import AsyncHTTPClient
2+
import Foundation
3+
import NIOCore
4+
import NIOFoundationCompat
5+
6+
struct Todo: Codable {
7+
var id: Int
8+
var name: String
9+
var completed: Bool
10+
}
11+
12+
@main
13+
struct PostJSON {
14+
static func main() async throws {
15+
let httpClient = HTTPClient(eventLoopGroupProvider: .singleton)
16+
let payload = Todo(id: 1, name: "Test Todo", completed: false)
17+
18+
do {
19+
let jsonData = try JSONEncoder().encode(payload)
20+
21+
var request = HTTPClientRequest(url: "http://localhost:8080/todos")
22+
request.method = .POST
23+
request.headers.add(name: "Content-Type", value: "application/json")
24+
request.body = .bytes(jsonData)
25+
26+
let response = try await httpClient.execute(request, timeout: .seconds(30))
27+
print("HTTP head", response)
28+
} catch {
29+
print("request failed:", error)
30+
}
31+
// it is important to shutdown the httpClient after all requests are done, even if one failed
32+
try await httpClient.shutdown()
33+
}
34+
}

Examples/PutJSON/PutJSON.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import AsyncHTTPClient
2+
import Foundation
3+
import NIOCore
4+
import NIOFoundationCompat
5+
6+
struct Todo: Codable {
7+
var id: Int
8+
var name: String
9+
var completed: Bool
10+
}
11+
12+
@main
13+
struct PostJSON {
14+
static func main() async throws {
15+
let httpClient = HTTPClient(eventLoopGroupProvider: .singleton)
16+
let payload = Todo(id: 1, name: "Test Todo", completed: true)
17+
18+
do {
19+
let jsonData = try JSONEncoder().encode(payload)
20+
21+
var request = HTTPClientRequest(url: "http://localhost:8080/todos/\(payload.id)")
22+
request.method = .PUT
23+
request.headers.add(name: "Content-Type", value: "application/json")
24+
request.body = .bytes(jsonData)
25+
26+
let response = try await httpClient.execute(request, timeout: .seconds(30))
27+
print("HTTP head", response)
28+
} catch {
29+
print("request failed:", error)
30+
}
31+
// it is important to shutdown the httpClient after all requests are done, even if one failed
32+
try await httpClient.shutdown()
33+
}
34+
}

0 commit comments

Comments
 (0)