Skip to content

Adding POST and GET examples #828

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

Closed
wants to merge 1 commit into from
Closed
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
52 changes: 0 additions & 52 deletions Examples/GetJSON/GetJSON.swift

This file was deleted.

109 changes: 109 additions & 0 deletions Examples/JSON/JSON.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the AsyncHTTPClient open source project
//
// Copyright (c) 2022 Apple Inc. and the AsyncHTTPClient project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of AsyncHTTPClient project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import AsyncHTTPClient
import Foundation
import NIOCore
import NIOFoundationCompat

struct Comic: Codable{
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
struct Comic: Codable{
struct Comic: Codable {

var num: Int
var title: String
var day: String
var month: String
var year: String
var img: String
var alt: String
var news: String
var link: String
var transcript: String
}

@main
struct JSON {

static func main() async throws {
try await getJSON()
try await postJSON()
}

static func getJSON() async throws {
let httpClient = HTTPClient(eventLoopGroupProvider: .singleton)
do {
let request = HTTPClientRequest(url: "https://xkcd.com/info.0.json")
let response = try await httpClient.execute(request, timeout: .seconds(30))
print("HTTP head", response)
let body = try await response.body.collect(upTo: 1024 * 1024) // 1 MB
// we use an overload defined in `NIOFoundationCompat` for `decode(_:from:)` to
// efficiently decode from a `ByteBuffer`
let comic = try JSONDecoder().decode(Comic.self, from: body)
dump(comic)
} 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()
}

static func postJSON() async throws {
let httpClient = HTTPClient(eventLoopGroupProvider: .singleton)

let comic: Comic = Comic(
num: 0,
title: "Adventures of Super Sally",
day: "17",
month: "4",
year: "2025",
img: "https://www.w3.org/Icons/w3c_main.png",
alt: "Adventures of Super Sally, a super hero with many powers",
news: "Today we learn about super heroes!",
link: "http://comics.com/super-sally",
transcript: "Once upon a time, there was a super hero named Super Sally. She had many powers and was a hero to many."
)

do {
var request = HTTPClientRequest(url: "https://httpbin.org/post")
request.headers.add(name: "Content-Type", value: "application/json")
request.headers.add(name: "Accept", value: "application/json")
request.method = .POST

let jsonData = try JSONEncoder().encode(comic)
request.body = .bytes(jsonData)

let response = try await httpClient.execute(request, timeout: .seconds(30))
let responseBody = try await response.body.collect(upTo: 1024 * 1024) // 1 MB
// we use an overload defined in `NIOFoundationCompat` for `decode(_:from:)` to
// efficiently decode from a `ByteBuffer`

// httpbin.org returns a JSON response with what we sent over the HTTP POST request,
// the json should be identical
let returnedComic = try JSONDecoder().decode(Comic.self, from: responseBody)
assert(comic.title == returnedComic.title)
assert(comic.img == returnedComic.img)
assert(comic.alt == returnedComic.alt)
assert(comic.day == returnedComic.day)
assert(comic.month == returnedComic.month)
assert(comic.year == returnedComic.year)
assert(comic.num == returnedComic.num)
assert(comic.transcript == returnedComic.transcript)
assert(comic.news == returnedComic.news)
assert(comic.link == returnedComic.link)
dump(returnedComic)
} 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()
}
}
6 changes: 3 additions & 3 deletions Examples/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ let package = Package(
],
products: [
.executable(name: "GetHTML", targets: ["GetHTML"]),
.executable(name: "GetJSON", targets: ["GetJSON"]),
.executable(name: "JSON", targets: ["JSON"]),
.executable(name: "StreamingByteCounter", targets: ["StreamingByteCounter"]),
],
dependencies: [
Expand All @@ -47,13 +47,13 @@ let package = Package(
path: "GetHTML"
),
.executableTarget(
name: "GetJSON",
name: "JSON",
dependencies: [
.product(name: "AsyncHTTPClient", package: "async-http-client"),
.product(name: "NIOCore", package: "swift-nio"),
.product(name: "NIOFoundationCompat", package: "swift-nio"),
],
path: "GetJSON"
path: "JSON"
),
.executableTarget(
name: "StreamingByteCounter",
Expand Down
7 changes: 4 additions & 3 deletions Examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ To run other examples you can just replace `GetHTML` with the name of the exampl
This examples sends a HTTP GET request to `https://apple.com/` and first `await`s and `print`s the HTTP Response Head.
Afterwards it buffers the full response body in memory and prints the response as a `String`.

## [GetJSON](./GetJSON/GetJSON.swift)
## [JSON](./JSON/JSON.swift)

This examples sends a HTTP GET request to `https://xkcd.com/info.0.json` and first `await`s and `print`s the HTTP Response Head.
Afterwards it buffers the full response body in memory, decodes the buffer using a `JSONDecoder` and `dump`s the decoded response.
This example demonstrates both GET and POST requests with JSON data:
1. It sends a HTTP GET request to `https://xkcd.com/info.0.json`, awaits and prints the HTTP Response Head, then buffers the full response body in memory, decodes the buffer using a `JSONDecoder` and dumps the decoded response.
2. It also performs a HTTP POST request to `https://httpbin.org/post` with a JSON payload, sending a Comic object and validating the response data matches what was sent.

## [StreamingByteCounter](./StreamingByteCounter/StreamingByteCounter.swift)

Expand Down
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,46 @@ HTTPClient.shared.execute(request: request).whenComplete { result in
}
```

#### Simple JSON request

See [Examples/JSON/JSON.swift](./Examples/JSON/JSON.swift) for a simple JSON HTTP GET and POST example.

```swift
import AsyncHTTPClient
import NIOFoundationCompat

struct Car: Codable {
var make: String
var model: String
var year: Int
}

static func getJSON() async throws {
let httpClient = HTTPClient(eventLoopGroupProvider: .singleton)
let car: Car = Car(make: "Toyota", model: "Camry", year: 2023)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This object is unused, and so this code snippet won't compile.

let request = HTTPClientRequest(url: "https://api.example.com/cars/123")
let response = try await httpClient.execute(request, timeout: .seconds(30))

let carData = try await response.body.collect(upTo: 1024 * 1024) // 1 MB
let car: Car = try JSONDecoder().decode(Car.self, from: carData)
try await httpClient.shutdown()
}

static func postJSON() async throws {
let httpClient = HTTPClient(eventLoopGroupProvider: .singleton)
let car: Car = Car(make: "Toyota", model: "Camry", year: 2023)
let jsonData = try JSONEncoder().encode(car)

var request = HTTPClientRequest(url: "https://api.example.com/cars/123")
request.headers.add(name: "Content-Type", value: "application/json")
request.headers.add(name: "Accept", value: "application/json")
request.method = .POST
request.body = .bytes(jsonData)
try await httpClient.shutdown()
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 doesn't send the request at all.

}
```


### Redirects following

The globally shared instance `HTTPClient.shared` follows redirects by default. If you create your own `HTTPClient`, you can enable the follow-redirects behavior using the client configuration:
Expand Down