Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
NextNext commit
feat: add http examples for POST PUT DELETE
  • Loading branch information
@iwa
iwa committed Dec 12, 2025
commit fc175272eebbdf20b86603bb432df6d09fc4c8c8
23 changes: 23 additions & 0 deletions Examples/DeleteJSON/DeleteJSON.swift
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
import AsyncHTTPClient
import Foundation
import NIOCore
import NIOFoundationCompat

@main
struct PostJSON{
static func main() async throws{
let httpClient = HTTPClient(eventLoopGroupProvider: .singleton)

do{
var request = HTTPClientRequest(url: "http://localhost:8080/todos/1)")
request.method = .DELETE

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()
}
}
30 changes: 30 additions & 0 deletions Examples/Package.swift
Original file line numberDiff line numberDiff line change
Expand Up@@ -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: [
Expand DownExpand Up@@ -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: [
Expand Down
34 changes: 34 additions & 0 deletions Examples/PostJSON/PostJSON.swift
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
import AsyncHTTPClient
import Foundation
import NIOCore
import NIOFoundationCompat

struct Todo: Codable{
var id: Int
var name: String
var completed: Bool
}

@main
struct PostJSON{
static func main() async throws{
let httpClient = HTTPClient(eventLoopGroupProvider: .singleton)
let payload = Todo(id: 1, name: "Test Todo", completed: false)

do{
let jsonData = try JSONEncoder().encode(payload)

var request = HTTPClientRequest(url: "http://localhost:8080/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()
}
}
34 changes: 34 additions & 0 deletions Examples/PutJSON/PutJSON.swift
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
import AsyncHTTPClient
import Foundation
import NIOCore
import NIOFoundationCompat

struct Todo: Codable{
var id: Int
var name: String
var completed: Bool
}

@main
struct PostJSON{
static func main() async throws{
let httpClient = HTTPClient(eventLoopGroupProvider: .singleton)
let payload = Todo(id: 1, name: "Test Todo", completed: true)

do{
let jsonData = try JSONEncoder().encode(payload)

var request = HTTPClientRequest(url: "http://localhost:8080/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()
}
}