I'm encountering an error with my POST API request: 'Error parsing JSON'
08:06 11 Apr 2024

I'm new to SwiftUI and having trouble posting an API request, I have checked everything but still getting 500 server error message. I'm stuck on this - please help if possible.

Response data:




  Server Error (500)


  

Server Error (500)

Error parsing JSON: Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around line 2, column 0." UserInfo={NSDebugDescription=Invalid value around line 2, column 0., NSJSONSerializationErrorIndex=1}

My code:

 do{

// Define the data to be sent
        let data: [String: String] = [
               "ad_id": "00000000-0000-0000-0000-000000000000",
               "device_id": "EE07C4CF-53CB-49D7-9991-111111111111",
               "device_token": "NOTFOUND"
           ]
    
    let encoder = JSONEncoder()
        // Convert the data dictionary to JSON data using JSONEncoder
        let jsonData = try encoder.encode(data)
        
     
        
        // Define the User-Agent header value
        let userAgentHeader = ["User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 15_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148"]
        
        // Convert User-Agent header dictionary to JSON string
        guard let headerData = try? JSONSerialization.data(withJSONObject: userAgentHeader, options: []),
              let headerJSON = String(data: headerData, encoding: .utf8) else {
            print("Failed to convert User-Agent header dictionary to JSON string")
            return
        }
                    
        // Create the request
        var request = URLRequest(url: url)
        request.httpMethod = "POST"
        
        // Define the headers dictionary
        let headers: [String: String] = [
            "Content-Type": "application/json",
            "User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 15_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148"
        ]

        // Add the headers to the request
        for (key, value) in headers {
            request.setValue(value, forHTTPHeaderField: key)
        }
        
        request.httpBody = jsonData
        
        if let reqString = String(data: jsonData, encoding: .utf8) {
            print("parameter data: \(reqString)")
        }
        
        // Perform the request using URLSession
        URLSession.shared.dataTask(with: request) { data, response, error in
            // Handle the response
            if let error = error {
                print("Error: \(error)")
                return
            }
            
            // Handle the data
            guard let data = data else {
                print("No data received")
                return
            }
            
            // Print the data as a string to see what you received
            if let responseString = String(data: data, encoding: .utf8) {
                print("Response data: \(responseString)")
            }
            
            // Parse the response data
            do {
                // Try parsing the JSON with allowFragments option
                let json = try JSONSerialization.jsonObject(with: data, options: [.mutableContainers, .allowFragments])
                print("Response JSON: \(json)")
            } catch {
                print("Error parsing JSON: \(error)")
            }
        }.resume()
    } catch {
        // Handle the encoding error
        print("Error encoding data to JSON: \(error)")
    }
ios json swift