From fdce29edf06e14e913182b50f072f2cd93527076 Mon Sep 17 00:00:00 2001 From: Navan Chauhan Date: Sun, 11 Feb 2024 07:54:24 -0700 Subject: final --- iCUrHealth/AutoCallerSheet.swift | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/iCUrHealth/AutoCallerSheet.swift b/iCUrHealth/AutoCallerSheet.swift index 6bad0ec..bfbacea 100644 --- a/iCUrHealth/AutoCallerSheet.swift +++ b/iCUrHealth/AutoCallerSheet.swift @@ -7,6 +7,45 @@ import SwiftUI +struct Student: Codable { + let phoneNumber: String + let name: String + let studentID: String + let dateOfBirth: String + let request: String +} + +func submitStudentInfo(student: Student) { + guard let url = URL(string: "http://127.0.0.1:8000/submit/") else { return } + + var request = URLRequest(url: url) + request.httpMethod = "POST" + request.addValue("application/json", forHTTPHeaderField: "Content-Type") + + do { + let jsonData = try JSONEncoder().encode(student) + request.httpBody = jsonData + + let task = URLSession.shared.dataTask(with: request) { data, response, error in + guard let data = data, error == nil else { + print("Error: \(error?.localizedDescription ?? "No error description.")") + return + } + + if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { + print("StatusCode should be 200, but is \(httpStatus.statusCode)") + print("Response = \(response!)") + } + + let responseString = String(data: data, encoding: .utf8) + print("responseString = \(responseString!)") + } + task.resume() + } catch { + print("Error: \(error.localizedDescription)") + } +} + struct AutoCallerSheet: View { @State private var helpNeeded: String -- cgit v1.2.3