-1

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

Andy Ibanez
  • 12,104
  • 9
  • 65
  • 100
JohnSF
  • 3,736
  • 3
  • 36
  • 72
  • You should try to intercept your requests and responses using something like MITMProxy to figure out if you are sending the data as you expect and what you are getting back. – Andy Ibanez Aug 17 '20 at 02:23
  • If you print out the url produce by `URLComponents` and the manually constructred one, you'd see that there are differences in certain paramters. This might be causing an error reply from the API that fails to decode as `ItemDataContainer` – New Dev Aug 17 '20 at 02:26

1 Answers1

1

You are missing path in component

components.path = "/VisualCrossingWebServices/rest/services/weatherdata/history"
k-thorat
  • 4,873
  • 1
  • 27
  • 36