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 1 commit 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
23 changes: 23 additions & 0 deletions Examples/DeleteJSON/DeleteJSON.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import AsyncHTTPClient
import Foundation
import NIOCore
import NIOFoundationCompat

@main
struct PostJSON {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example does a DELETE ;)

static func main() async throws {
let httpClient = HTTPClient(eventLoopGroupProvider: .singleton)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can actually use HTTPClient.shared instead here which you don't need to shutdown


do {
var request = HTTPClientRequest(url: "http://localhost:8080/todos/1)")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I generally think it's best of examples can be run without the user needing to do anything extra like run a local server. Had you thought about using something like httpbin instead?

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 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
34 changes: 34 additions & 0 deletions Examples/PostJSON/PostJSON.swift
Original file line number Diff line number Diff 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 number Diff line number Diff 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()
}
}