Skip to content

Add HTTP examples for POST PUT DELETE #849

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
21 changes: 21 additions & 0 deletions Examples/DeleteJSON/DeleteJSON.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
30 changes: 30 additions & 0 deletions Examples/Package.swift
Original file line number Diff line number Diff 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 Down Expand 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
35 changes: 35 additions & 0 deletions Examples/PostJSON/PostJSON.swift
Original file line number Diff line number Diff line change
@@ -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()
}
}
35 changes: 35 additions & 0 deletions Examples/PutJSON/PutJSON.swift
Original file line number Diff line number Diff line change
@@ -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()
}
}