如何在 SwiftUI 中从 URLSession 获取响应
How to Get Response from URLSession in SwiftUI
我正在尝试在 SwiftUI 中构建一个 URLSession 来处理 API 基于令牌的身份验证以登录到网络应用程序。
我能够让 API 在 Postman 中正常工作,但无法在 SwiftUI 中获得任何类型的可读响应。我是 SwiftUI 和调试的新手。从我处理授权的网络服务器日志来看,我的 Swift 代码的登录请求似乎是成功的,但我似乎无法解析出 body 或 headers .
我的 canvas.
中不断收到“LoginResponse(message: nil)”
这是我的 SwiftUI 代码,简化为仅尝试打印 URLSession 响应。
import Foundation
enum AuthenticationError: Error {
case invalidCredentials
case custom(errorMessage: String)
}
struct LoginRequestBody: Codable {
let email: String
let password: String
}
struct LoginResponse: Codable {
let message: String?
}
class Sessionservice {
func login(emailAddress: String, password: String, completion: @escaping (Result<String, AuthenticationError>) -> Void){
guard let url = URL(string: "https://www.example.com/api/v1/auth/sign_in") else {
completion(.failure(.custom(errorMessage:"Site is unavailable. Try again later.")))
return
}
let body = LoginRequestBody(email: "email@example.com", password: "password")
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = try? JSONEncoder().encode(body)
URLSession.shared.dataTask(with: request) { (data, response, error) in
guard let data = data, error == nil else {
completion(.failure(.custom(errorMessage: "No data")))
return
}
try! JSONDecoder().decode(LoginResponse.self, from: data)
guard let loginResponse = try? JSONDecoder().decode(LoginResponse.self, from: data) else {
completion(.failure(.custom(errorMessage: "loginResponse Failure")))
return
}
print(loginResponse)
}.resume()
}
}
import Foundation
import SwiftUI
enum AuthenticationError: Error {
case invalidCredentials
case custom(errorMessage: String)
}
struct LoginRequestBody: Codable {
let email: String
let password: String
}
struct LoginResponse: Decodable {
let data: LoginValue?
let success: Bool?
let client: String?
}
struct LoginToken: Decodable {
let token: String?
}
struct LoginValue: Decodable {
let message: String?
let user: UserRole?
let success: Bool?
}
struct UserRole: Decodable {
let role: String?
}
class Sessionservice {
func login(email: String, password: String, completion: @escaping (Result<String, AuthenticationError>) -> Void){
guard let url = URL(string: "https://www.example.com/api/v1/auth/sign_in") else {
completion(.failure(.custom(errorMessage:"Site is unavailable. Try again later.")))
return
}
let body = LoginRequestBody(email: email, password: password)
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = try? JSONEncoder().encode(body)
URLSession.shared.dataTask(with: request) { (data, response, error) in
if let response = response as? HTTPURLResponse {
guard let token = response.value(forHTTPHeaderField: "Accesstoken") else {
completion(.failure(.custom(errorMessage: "Missing Access Token")))
return
}
completion(.success(token))
}
guard let data = data, error == nil else { return }
guard let loginResponse = try? JSONDecoder().decode(LoginResponse.self, from: data) else { return }
guard let messageData = loginResponse.data else {return}
guard let message = messageData.message else {return}
guard let userRole = messageData.user else {return}
guard let role = userRole.role else {return}
completion(.success(message))
completion(.success(role))
}.resume()
}
}
我正在尝试在 SwiftUI 中构建一个 URLSession 来处理 API 基于令牌的身份验证以登录到网络应用程序。
我能够让 API 在 Postman 中正常工作,但无法在 SwiftUI 中获得任何类型的可读响应。我是 SwiftUI 和调试的新手。从我处理授权的网络服务器日志来看,我的 Swift 代码的登录请求似乎是成功的,但我似乎无法解析出 body 或 headers .
我的 canvas.
中不断收到“LoginResponse(message: nil)”这是我的 SwiftUI 代码,简化为仅尝试打印 URLSession 响应。
import Foundation
enum AuthenticationError: Error {
case invalidCredentials
case custom(errorMessage: String)
}
struct LoginRequestBody: Codable {
let email: String
let password: String
}
struct LoginResponse: Codable {
let message: String?
}
class Sessionservice {
func login(emailAddress: String, password: String, completion: @escaping (Result<String, AuthenticationError>) -> Void){
guard let url = URL(string: "https://www.example.com/api/v1/auth/sign_in") else {
completion(.failure(.custom(errorMessage:"Site is unavailable. Try again later.")))
return
}
let body = LoginRequestBody(email: "email@example.com", password: "password")
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = try? JSONEncoder().encode(body)
URLSession.shared.dataTask(with: request) { (data, response, error) in
guard let data = data, error == nil else {
completion(.failure(.custom(errorMessage: "No data")))
return
}
try! JSONDecoder().decode(LoginResponse.self, from: data)
guard let loginResponse = try? JSONDecoder().decode(LoginResponse.self, from: data) else {
completion(.failure(.custom(errorMessage: "loginResponse Failure")))
return
}
print(loginResponse)
}.resume()
}
}
import Foundation
import SwiftUI
enum AuthenticationError: Error {
case invalidCredentials
case custom(errorMessage: String)
}
struct LoginRequestBody: Codable {
let email: String
let password: String
}
struct LoginResponse: Decodable {
let data: LoginValue?
let success: Bool?
let client: String?
}
struct LoginToken: Decodable {
let token: String?
}
struct LoginValue: Decodable {
let message: String?
let user: UserRole?
let success: Bool?
}
struct UserRole: Decodable {
let role: String?
}
class Sessionservice {
func login(email: String, password: String, completion: @escaping (Result<String, AuthenticationError>) -> Void){
guard let url = URL(string: "https://www.example.com/api/v1/auth/sign_in") else {
completion(.failure(.custom(errorMessage:"Site is unavailable. Try again later.")))
return
}
let body = LoginRequestBody(email: email, password: password)
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = try? JSONEncoder().encode(body)
URLSession.shared.dataTask(with: request) { (data, response, error) in
if let response = response as? HTTPURLResponse {
guard let token = response.value(forHTTPHeaderField: "Accesstoken") else {
completion(.failure(.custom(errorMessage: "Missing Access Token")))
return
}
completion(.success(token))
}
guard let data = data, error == nil else { return }
guard let loginResponse = try? JSONDecoder().decode(LoginResponse.self, from: data) else { return }
guard let messageData = loginResponse.data else {return}
guard let message = messageData.message else {return}
guard let userRole = messageData.user else {return}
guard let role = userRole.role else {return}
completion(.success(message))
completion(.success(role))
}.resume()
}
}