Тот, который не использует быстрый код. Все значения параметров установлены и действительны. Показываю мой метод POST, но GET также завершается с ошибкой 503.
Код: Выделить всё
private func post() async throws {
let rcid = UserDefaults.standard.value(forKey: DefaultKey.rcID.rawValue) as? String ?? "NOT SET"
guard let url = URL(string: "https://example.com/folder/subfolder/insertUserIntoDBPOST.php") else {
throw URLError(.badURL)
}
var request = URLRequest(url: url)
request.httpMethod = "POST"
let parameters = [
URLQueryItem(name: "Line", value: selectedLine),
URLQueryItem(name: "Depot", value: selectedDepot),
URLQueryItem(name: "RCID", value: rcid),
URLQueryItem(name: "RemainingFreeLaunches", value: String(remainingFreeLaunches)),
URLQueryItem(name: "SubscriptionStatus", value: subscriptionDuration.rawValue)
]
let parameterString = parameters.map { "\($0.name)=\($0.value ?? "")" }.joined(separator: "&")
request.httpBody = parameterString.data(using: .utf8)
do {
let (data, response) = try await URLSession.shared.data(for: request)
guard let httpResponse = response as? HTTPURLResponse else {
throw URLError(.badServerResponse)
}
if httpResponse.statusCode == 200 {
// do something with a successful update
} else {
/// throw the error (for eg 503)
throw URLError(.badServerResponse)
}
} catch {
throw error
}
}
Код: Выделить всё
быстрый рабочий код
Код: Выделить всё
func insertNewSecret(secretClaimedBy: String, type: SecretType) async throws {
guard let insertSecretURL = URL(string: "https://example.com/Folder/Subfolder/secretEntryFile.php?secretType=\(type.rawValue)&secretClaimedBy=\(secretClaimedBy)") else { throw SCError.invalidURL }
var request = URLRequest(url: insertSecretURL)
request.httpMethod = "GET"
do {
let (_, _) = try await URLSession.shared.data(for: request)
}
catch {
//handle error
}
}
Код: Выделить всё
Подробнее здесь: [url]https://stackoverflow.com/questions/79244017/trying-to-update-database-from-swift-urlsession-returns-503[/url]