-
Notifications
You must be signed in to change notification settings - Fork 49
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
Comments
Can you give more specifics? What classes and methods of TRON you've used to fetch file? |
Hi, there
Here are the source code and the error message. Hope these can give you enough info.
import Foundation
import TRON
import SwiftyJSON
struct Service {
let tron = TRON(baseURL: "https://letsbuildthatapp-videos.s3-us-west-2.amazonaws.com <https://letsbuildthatapp-videos.s3-us-west-2.amazonaws.com/>")
static let sharedInstance = Service()
func fetchHomeFeed(completion: @escaping (HomeDatasource) -> ()) {
let request: APIRequest<HomeDatasource, JSONError> = tron.request("/kindle.json")
request.perform(withSuccess: { (homeDatasource) in
completion(homeDatasource)
}) { (err) in
print("Failed to fetch json...", err)
}
}
class JSONError: JSONDecodable {
required init(json: JSON) throws {
print("JSON ERROR")
}
}
}
Failed to fetch json...
Failed to fetch json... APIError<JsonError>(request: Optional(https://letsbuildthatapp-videos.s3-us-west-2.amazonaws.com/kindle.json), response: Optional(<NSHTTPURLResponse: 0x608000036dc0> { URL: https://letsbuildthatapp-videos.s3-us-west-2.amazonaws.com/kindle.json } { status code: 200, headers {
"Accept-Ranges" = bytes;
"Content-Length" = 5435;
"Content-Type" = "application/octet-stream";
Date = "Fri, 24 Feb 2017 14:28:36 GMT";
Etag = "\"f20ccea4a6d3d5ab5d82bb65bb1410b8\"";
"Last-Modified" = "Tue, 10 Jan 2017 04:55:47 GMT";
Server = AmazonS3;
"x-amz-id-2" = "RM7lSU+tXYiKJ72dsYEJceVqtaJkgLWWEYjgzMnN4VmrLSGhkOP5vjsd/zUZR0ypeQA8sui/Hho=";
"x-amz-request-id" = 608CC807B2C85B3E;
} }), data: Optional(5435 bytes), error: Optional(Alamofire.AFError.responseValidationFailed(Alamofire.AFError.ResponseValidationFailureReason.unacceptableContentType(["application/json"], "text/plain"))), errorModel: Optional(KindleLBTA.Service.JsonError))
… Denys Telezhkin ***@***.***> 於 2017年2月24日 下午7:23 寫道:
Can you give more specifics? What classes and methods of TRON you've used to fetch file?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub <#26 (comment)>, or mute the thread <https://github.com/notifications/unsubscribe-auth/ARgzY0Zapp7UYLh6IA57JNhY9Q2Hmdhsks5rfr2zgaJpZM4MK8sm>.
|
There's couple of things is noticed:
Make sure your request has appropriate headers that server expects. |
static let sharedInstance = Service()
} |
the homeDatasource does accept array and work correctly.
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 <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()
}
… 吳德仁 ***@***.*** ***@***.***>> 於 2017年2月24日 下午10:41 寫道:
Hi, there
Here are the source code and the error message. Hope these can give you enough info.
import Foundation
import TRON
import SwiftyJSON
struct Service {
let tron = TRON(baseURL: "https://letsbuildthatapp-videos.s3-us-west-2.amazonaws.com <https://letsbuildthatapp-videos.s3-us-west-2.amazonaws.com/>")
static let sharedInstance = Service()
func fetchHomeFeed(completion: @escaping (HomeDatasource) -> ()) {
let request: APIRequest<HomeDatasource, JSONError> = tron.request("/kindle.json")
request.perform(withSuccess: { (homeDatasource) in
completion(homeDatasource)
}) { (err) in
print("Failed to fetch json...", err)
}
}
class JSONError: JSONDecodable {
required init(json: JSON) throws {
print("JSON ERROR")
}
}
}
Failed to fetch json...
Failed to fetch json... APIError<JsonError>(request: Optional(https://letsbuildthatapp-videos.s3-us-west-2.amazonaws.com/kindle.json <https://letsbuildthatapp-videos.s3-us-west-2.amazonaws.com/kindle.json>), response: Optional(<NSHTTPURLResponse: 0x608000036dc0> { URL: https://letsbuildthatapp-videos.s3-us-west-2.amazonaws.com/kindle.json <https://letsbuildthatapp-videos.s3-us-west-2.amazonaws.com/kindle.json> } { status code: 200, headers {
"Accept-Ranges" = bytes;
"Content-Length" = 5435;
"Content-Type" = "application/octet-stream";
Date = "Fri, 24 Feb 2017 14:28:36 GMT";
Etag = "\"f20ccea4a6d3d5ab5d82bb65bb1410b8\"";
"Last-Modified" = "Tue, 10 Jan 2017 04:55:47 GMT";
Server = AmazonS3;
"x-amz-id-2" = "RM7lSU+tXYiKJ72dsYEJceVqtaJkgLWWEYjgzMnN4VmrLSGhkOP5vjsd/zUZR0ypeQA8sui/Hho=";
"x-amz-request-id" = 608CC807B2C85B3E;
} }), data: Optional(5435 bytes), error: Optional(Alamofire.AFError.responseValidationFailed(Alamofire.AFError.ResponseValidationFailureReason.unacceptableContentType(["application/json"], "text/plain"))), errorModel: Optional(KindleLBTA.Service.JsonError))
> Denys Telezhkin ***@***.*** ***@***.***>> 於 2017年2月24日 下午7:23 寫道:
>
> Can you give more specifics? What classes and methods of TRON you've used to fetch file?
>
> —
> You are receiving this because you authored the thread.
> Reply to this email directly, view it on GitHub <#26 (comment)>, or mute the thread <https://github.com/notifications/unsubscribe-auth/ARgzY0Zapp7UYLh6IA57JNhY9Q2Hmdhsks5rfr2zgaJpZM4MK8sm>.
>
|
Maybe the problem is with default headers we are setting on tron.headerBuilder = HeaderBuilder(defaultHeaders: [:]) And only after that: let request: APIRequest<HomeDatasource, JSONError> = tron.request("/kindle.json") |
After adding the setting, the json file can be transmitted correctly. Thanks a lot for your help! |
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
The text was updated successfully, but these errors were encountered: