0
    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 :).

Alexander Choi
  • 101
  • 1
  • 1
  • 6
  • Decode the JSON to an object. Then add the new content to the object. Then encode the object back to JSON. // Each one of these steps has many examples already on the site, please do some research, it's easy to find the solution. The key is to break down your goal into smaller sub tasks. – Eric Aya Aug 04 '21 at 15:08
  • Thank you for the help, but I'm still confused on how to actually replace the data in my localhost file with my new JSON data. I encoded back into JSON like this: let wer = try JSONEncoder().encode(parsedJSON) let json = String(data: wer, encoding: .utf8) – Alexander Choi Aug 06 '21 at 02:32
  • First issue: why are you converting your JSON object to String with `let json = String(data: wer, encoding: .utf8)`? You never need to do that except maybe for printing while debugging. Your JSON data is `wer`, you have it, no need for further transformations. Then, if you want to save it... just do that. Save it to a local file. Many tutorials about how to do that. – Eric Aya Aug 06 '21 at 08:44
  • I appreciate you helping me, and I apologize for asking so many questions. I updated my question with my new code to look at. Basically, its printing out the new JSON file, but the post request doesn't seem to be working. It says "200" not "201" so data isn't being uploaded. – Alexander Choi Aug 07 '21 at 15:42

0 Answers0