From 6cb4b75f82eefc031494b7fdf7c8b362ae5aff6a Mon Sep 17 00:00:00 2001 From: Hykilpikonna Date: Fri, 22 Jan 2021 16:14:02 -0500 Subject: [PATCH] [F] Fix trying to parse string values as jsons --- ProjectClock/Net.swift | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ProjectClock/Net.swift b/ProjectClock/Net.swift index 478c481..0e0e981 100644 --- a/ProjectClock/Net.swift +++ b/ProjectClock/Net.swift @@ -7,7 +7,7 @@ import Foundation /// Base URL of the HTTP server -let baseUrl = "http://localhost:8080/api" // TODO: Production settings +let baseUrl = "http://localhost:8080" // TODO: Production settings /// Json class class JSON @@ -212,6 +212,9 @@ func send(_ api: API, _ params: [String: String]? = [:], _ succ // If success if (200...299).contains(response.statusCode) { + // If the desired type is string, it doesn't have to parse json. + if T.self == String.self, let msg = String(data: raw, encoding: .utf8) { success(msg as! T); return } + // Parse JSON guard let obj = try? JSON.decoder.decode(T.self, from: raw) else { err("JSON cannot be parsed"); return }