-
Notifications
You must be signed in to change notification settings - Fork 128
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 { | ||
static func main() async throws { | ||
let httpClient = HTTPClient(eventLoopGroupProvider: .singleton) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can actually use |
||
|
||
do { | ||
var request = HTTPClientRequest(url: "http://localhost:8080/todos/1)") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
} | ||
} |
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() | ||
} | ||
} |
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() | ||
} | ||
} |
There was a problem hiding this comment.
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
;)