URLSession dataTask method returns 0 bytes of data

时光毁灭记忆、已成空白 提交于 2021-02-10 16:14:07

问题


I am using URLSession's dataTask method with a completion handler. The error in response is nil, but the data object returns something, it returns 0 bytes of data.

I was using Alamofire library firstly, I thought there is something wrong with it because I started using newer version so I stated using my own implementation of Alamofire just so I don't have to rewrite all the calls I already have in my app.

It still returns 0 bytes of data.

When I use the same URL in the Playground with a simple URLSession call, it works and returns the data, do you have any idea what might go wrong?

My implementation of Alamofire (Srsly quick and dirty within 30 minutes):

import Foundation

typealias DataResponse = (data: Data?, response: URLResponse?, error: Error?, request: URLRequest?, result: Result)

public class Alamofire: NSObject {

    enum Method: String {
        case get = "GET"
        case post = "POST"
    }

    fileprivate static let session = URLSession(configuration: URLSessionConfiguration.default)

    public struct request {
        var request: URLRequest? = nil
        var url: URL? = nil
        let method: Alamofire.Method?
        let parameters: Parameters?
        let headers: Headers?

        init(_ request: URLRequest, method: Alamofire.Method? = nil, parameters: Parameters? = nil, headers: Headers? = nil) {
            self.request = request
            self.url = request.url
            self.method = method
            self.parameters = parameters
            self.headers = headers
        }

        init(_ url: URL, method: Alamofire.Method? = nil, parameters: Parameters? = nil, headers: Headers? = nil) {
            self.url = url
            self.request = URLRequest(url: url)
            self.method = method
            self.parameters = parameters
            self.headers = headers
        }
    }
}

typealias Parameters = [String: Any?]
typealias Headers = [String: String]

extension Alamofire.request {

    func responseData(completionHandler: @escaping (_ dataResponse: DataResponse) -> Void) {
        guard let request = request else { return }

        Alamofire.session.dataTask(with: request) { (data, response, error) in
            let result: Result
            if let error = error {
                result = Result.failure(error)
                completionHandler((data, response, error, request, result))
                return
            }

            completionHandler((data, response, error, request, Result.success))
        }.resume()
    }   
}

enum Result {
    case success
    case failure(Error)
}

回答1:


So the resolution to my problem was a realisation that the data provider cut me off of data :) Can happen too. That's probably not a "solution" but an option you have to consider as I will from now on :D Thank you all




回答2:


Did you create an App Transport Security Execption for Allows Arbitrary loads in the info.plist?



来源:https://stackoverflow.com/questions/48172103/urlsession-datatask-method-returns-0-bytes-of-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!