iosjsonxcodeurlcomponents

JSON URLComponents URLQueryItem Failure


I'm struggling with an http call for json data. If I use the entire URL as a string, the json fetch works. If I separate the call into URLComponents I have not been able to make it work.

My Webservice:

final class Webservice {

      var components: URLComponents {
          var components = URLComponents()
          components.scheme = "https"
          components.host = "weather.visualcrossing.com"

          components.queryItems =
              [
              URLQueryItem(name: "aggregateHours", value: "24"),
              URLQueryItem(name: "combinationMethod", value: "aggregate"),
              URLQueryItem(name: "startDateTime", value: "2020-08-03T00:00:00"),
              URLQueryItem(name: "endDateTime", value: "2020-08-10T00:00:00"),
          
              URLQueryItem(name: "collectStationContributions", value: "false"),
              URLQueryItem(name: "maxStations", value: "-1"),
              URLQueryItem(name: "maxDistance", value: "-1"),
              URLQueryItem(name: "includeNormals", value: "false"),
          
              URLQueryItem(name: "contentType", value: "json"),
              URLQueryItem(name: "unitGroup", value: "us"),
              URLQueryItem(name: "locationMode", value: "single"),
              URLQueryItem(name: "key", value: "myPersonalKeyHere"),
          
              URLQueryItem(name: "locations", value: "Findlay, OH"),
              ]
          return components
      }

    let myURL = URL(string: "https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/weatherdata/history?aggregateHours=24&combinationMethod=aggregate&startDateTime=2020-08-03T00%3A00%3A00&endDateTime=2020-08-10T00%3A00%3A00&collectStationContributions=false&maxStations=-1&maxDistance=-1&includeNormals=false&contentType=json&unitGroup=us&locationMode=single&key=myPersonalKeyHere&locations=Findlay%2C%20oh")

    func fetchItems() -> AnyPublisher<ItemDataContainer, Error> {
    
        //fix this - do not force unwrap
        
        //Using myURL - it works
        //return URLSession.shared.dataTaskPublisher(for: myURL!)
        
        //using the components url it fails
        return URLSession.shared.dataTaskPublisher(for: components.url!)
            .map { $0.data }
            .decode(type: ItemDataContainer.self, decoder: JSONDecoder())
            .receive(on: DispatchQueue.main)
            .eraseToAnyPublisher()
    }//fetch

}//class

This is the error:

received an error: , dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: "The given data was not valid JSON.", underlyingError: Optional(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.})))

Any guidance would be appreciated. Xcode 11.6, iOS 13.6


Solution

  • You are missing path in component

    components.path = "/VisualCrossingWebServices/rest/services/weatherdata/history"