-
Notifications
You must be signed in to change notification settings - Fork 123
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
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
This file was deleted.
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{ | ||
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() | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
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. 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() | ||
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. 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: | ||
|
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.