let parameters = ["lat": latitude, "lng": longitude]
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-type")
guard let httpBody = try? JSONSerialization.data(withJSONObject: parameters, options: []) else { return }
request.httpBody = httpBody
let session = URLSession.shared
session.dataTask(with: request) { (data, response, error) in
if let response = response {
print(response)
}
if let httpResponse = response as? HTTPURLResponse {
print(httpResponse.statusCode)
}
if let data = data {
do {
let jsonDecoder = JSONDecoder()
var parsedJSON = try jsonDecoder.decode([UserCoords].self, from: data)
parsedJSON.append(Vac_Pass.UserCoords(lat: latitude, lng: longitude))
let wer = try? JSONEncoder().encode(parsedJSON)
let json = try JSONSerialization.jsonObject(with: wer!, options: [])
print(json)
print(json)
} catch {
print(error)
}
}
}.resume()
Since the http post response says "200," this indicates that the request is successful but is not successful in uploading the new data. If the data did get uploaded, it would print out "201". I think there may be something wrong with my localhost web server.
The localhost is essentially a JSON file, which contains coordinates like this:
[
{"lat" : 38.8976, "lng" : -77.0369 } ,
{"lat" : 38.8980, "lng" : -77.0363 } ,
{"lat" : 38.8990, "lng" : -77.0367 }
]
Thank you - all inputs are appreciated :).