Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

the error when fetching some json file #26

Closed
wtj123 opened this issue Feb 24, 2017 · 7 comments
Closed

the error when fetching some json file #26

wtj123 opened this issue Feb 24, 2017 · 7 comments
Assignees
Labels

Comments

@wtj123
Copy link

wtj123 commented Feb 24, 2017

When using TRON to fetch the json file: "https://letsbuildthatapp-videos.s3-us-west-2.amazonaws.com/kindle.json", there is an error occurred but the response code is still 200. Nonetheless, if I used the URLSession.shared.dataTask() instead, the json file is transmitted correctly.

Te-Jen Wu
macpw123@ms4.hinet.net

@DenTelezhkin
Copy link
Member

Can you give more specifics? What classes and methods of TRON you've used to fetch file?

@DenTelezhkin DenTelezhkin self-assigned this Feb 24, 2017
@wtj123
Copy link
Author

wtj123 commented Feb 24, 2017 via email

@DenTelezhkin
Copy link
Member

There's couple of things is noticed:

  1. JSON you mentioned has Array in its root, not Dictionary. Does HomeDatasource object accept array?
  2. Error is coming from Alamofire - Optional(Alamofire.AFError.responseValidationFailed(Alamofire.AFError.ResponseValidationFailureReason.unacceptableContentType(["application/json"], "text/plain")))

Make sure your request has appropriate headers that server expects.

@wtj123
Copy link
Author

wtj123 commented Feb 25, 2017

  1. the homeDatasource does accept array and work correctly.
  2. I used the URLSession.shared.dataTask() instead to fetch the same URL and can get the json file without any errors. the source code is below:

static let sharedInstance = Service()

func fetchHomeFeed(completion: @escaping (HomeDatasource)->()) {
    
    let url = URL(string: "https://letsbuildthatapp-videos.s3-us-west-2.amazonaws.com/kindle.json")
    
    URLSession.shared.dataTask(with: url!) { (data, response, error) in
        if error != nil {
            print("failed to fetch data...")
            return
        }
        guard let data = data else {return}
        
        let json = JSON(data: data)
        do {
            let homeDatasource = try HomeDatasource(json: json)
            
            DispatchQueue.main.async(execute: {
                completion(homeDatasource)
            })
            
        } catch {
            print("ERROR")
        }
    }.resume()

}

@wtj123
Copy link
Author

wtj123 commented Feb 25, 2017 via email

@DenTelezhkin
Copy link
Member

Maybe the problem is with default headers we are setting on HeaderBuilder class. Can you try following before creating request?

tron.headerBuilder = HeaderBuilder(defaultHeaders: [:])

And only after that:

let request: APIRequest<HomeDatasource, JSONError> = tron.request("/kindle.json")

@wtj123
Copy link
Author

wtj123 commented Feb 25, 2017

After adding the setting, the json file can be transmitted correctly. Thanks a lot for your help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants