Any way to get the response body during HTTP errors?

前端 未结 2 1651
谎友^
谎友^ 2020-12-13 23:36

I\'m hitting an API that will occasionally throw a HTTP 403 error, and the response body can give some extra information in the form of json, however for the life of me I ca

2条回答
  •  没有蜡笔的小新
    2020-12-14 00:11

    I asked this question on their github page and got an answer from cnoon:

    swift 2:

    if let data = response.data {
        let json = String(data: data, encoding: NSUTF8StringEncoding)
        print("Failure Response: \(json)")
    }
    

    swift 3:

    if let data = response.data {
        let json = String(data: data, encoding: String.Encoding.utf8)
        print("Failure Response: \(json)")
    }
    

    https://github.com/Alamofire/Alamofire/issues/1059

    I just left out the encoding part, by doing this you are able to get the response json even in the case of errors.

提交回复
热议问题