为什么我只在Swift中获得响应头

时间:2019-04-24 15:17:58

标签: swift api http header nsurlsession

我正在尝试从网站获取json,并且正在使用Swift。

为什么我只得到响应头而不是正文?

    let headers = [
        "auth": key,
        "Cache-Control": "no-cache",
    ]

    let request = NSMutableURLRequest(url: NSURL(string: url)! as URL,
                                      cachePolicy: .useProtocolCachePolicy,
                                      timeoutInterval: 10.0)
    request.httpMethod = "GET"
    request.allHTTPHeaderFields = headers

    let session = URLSession.shared
    let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
        if (error != nil) {
            print(error!)
        } else {
            let httpResponse = response as? HTTPURLResponse
            print(httpResponse!)

        }
    })

    dataTask.resume()

我该如何解决?

0 个答案:

没有答案