AlamofireImage 代码=-1016 "Failed to validate response due to unacceptable content type"
AlamofireImage Code=-1016 "Failed to validate response due to unacceptable content type"
我使用 Alamofire 和 SwityJSON 解析从 API 获得的响应,获得了图像的 URL。后来,在我的代码中,我尝试使用 AlamofireImage 将该图像设置为 ImageView...我正在尝试执行以下操作:
let headers = ["Authorization": requestToken, "Content-Type": "image/jpg"]
print(foods[indexPath.row].image)
Alamofire.request(.GET, imageEndPoint, headers: headers)
.responseImage { response in
debugPrint(response)
print(response.request)
print(response.response)
if let image = response.result.value {
print("image downloaded: \(image)")
}
}
但是,在调试时,出现以下错误
Request]: <NSMutableURLRequest: 0x7fa93e052e60> { URL: http://159.203.92.55:9000/api/media/image?path=./server/uploads/images/products/jOqmy768a5wN2tcPd07cPhVH.jpg }
[Response]: <NSHTTPURLResponse: 0x7fa93e05a0a0> { URL: http://159.203.92.55:9000/api/media/image?path=./server/uploads/images/products/jOqmy768a5wN2tcPd07cPhVH.jpg } { status code: 200, headers {
Connection = "keep-alive";
"Content-Type" = "image/jpg";
Date = "Sun, 17 Jan 2016 16:28:38 GMT";
"Transfer-Encoding" = Identity;
"X-Powered-By" = Express;
} }
[Data]: 26224 bytes
[Result]: FAILURE: Error Domain=com.alamofire.error Code=-1016 "Failed to validate response due to unacceptable content type" UserInfo={NSLocalizedFailureReason=Failed to validate response due to unacceptable content type}
Optional(<NSMutableURLRequest: 0x7fa93e052e60> { URL: http://159.203.92.55:9000/api/media/image?path=./server/uploads/images/products/jOqmy768a5wN2tcPd07cPhVH.jpg })
显然是因为内容类型的问题,但我不知道如何解决:
[Result]: FAILURE: Error Domain=com.alamofire.error Code=-1016 "Failed to validate response due to unacceptable content type" UserInfo={NSLocalizedFailureReason=Failed to validate response due to unacceptable content type}
由于这个错误,我无法继续,只能将图像设置到 imageView
cell.thumbnailImageView.image = myImage
当我尝试使用 Chrome 的 Postman 执行相同的请求时,我得到的图像没有任何问题。
有什么建议吗?谢谢
更新:我找到了一种与我想要的相去甚远但有效的方法
Alamofire.request(.GET, foods[indexPath.row].image, headers: headers)
.response { (request, response, data, error) in
debugPrint(response)
cell.thumbnailImageView.image = UIImage(data: data!, scale: 1)
}
这是香草 Alamofire,所以我仍然无法使用 AlamofireImage,它为图像提供了更好的支持。我从以下 post 得到这个方法: How to load image in swift using Alamofire (skywinder 的回答)
您输入的内容类型有误。应该是 image/jpeg
(http://www.sitepoint.com/web-foundations/mime-types-complete-list/).
let headers = ["Authorization": requestToken, "Content-Type": "image/jpeg"]
我希望这能解决问题。
所以我注意到服务器的答案是 "Content-Type" = "image/jpg"; 但正如用户@Gomines 指出的那样,正确的内容类型为alamofireImage 实际上是 "Content-Type": "image/jpeg"
然后我回到 AlamofireImages 文档并阅读了这篇文章
If the image you are attempting to download is an invalid MIME type not in the list, you can add custom acceptable content types using the addAcceptableImageContentTypes extension on the Request type.
所以我只需要在 AlamofireImage 请求之前添加这行代码:
Request.addAcceptableImageContentTypes(["image/jpg"])
问题是我没有匹配服务器响应的相同内容类型。但是,该行解决了问题,因为现在 AlamofireImage 也将接受服务器发送的相同类型。
我使用 Alamofire 和 SwityJSON 解析从 API 获得的响应,获得了图像的 URL。后来,在我的代码中,我尝试使用 AlamofireImage 将该图像设置为 ImageView...我正在尝试执行以下操作:
let headers = ["Authorization": requestToken, "Content-Type": "image/jpg"]
print(foods[indexPath.row].image)
Alamofire.request(.GET, imageEndPoint, headers: headers)
.responseImage { response in
debugPrint(response)
print(response.request)
print(response.response)
if let image = response.result.value {
print("image downloaded: \(image)")
}
}
但是,在调试时,出现以下错误
Request]: <NSMutableURLRequest: 0x7fa93e052e60> { URL: http://159.203.92.55:9000/api/media/image?path=./server/uploads/images/products/jOqmy768a5wN2tcPd07cPhVH.jpg }
[Response]: <NSHTTPURLResponse: 0x7fa93e05a0a0> { URL: http://159.203.92.55:9000/api/media/image?path=./server/uploads/images/products/jOqmy768a5wN2tcPd07cPhVH.jpg } { status code: 200, headers {
Connection = "keep-alive";
"Content-Type" = "image/jpg";
Date = "Sun, 17 Jan 2016 16:28:38 GMT";
"Transfer-Encoding" = Identity;
"X-Powered-By" = Express;
} }
[Data]: 26224 bytes
[Result]: FAILURE: Error Domain=com.alamofire.error Code=-1016 "Failed to validate response due to unacceptable content type" UserInfo={NSLocalizedFailureReason=Failed to validate response due to unacceptable content type}
Optional(<NSMutableURLRequest: 0x7fa93e052e60> { URL: http://159.203.92.55:9000/api/media/image?path=./server/uploads/images/products/jOqmy768a5wN2tcPd07cPhVH.jpg })
显然是因为内容类型的问题,但我不知道如何解决:
[Result]: FAILURE: Error Domain=com.alamofire.error Code=-1016 "Failed to validate response due to unacceptable content type" UserInfo={NSLocalizedFailureReason=Failed to validate response due to unacceptable content type}
由于这个错误,我无法继续,只能将图像设置到 imageView
cell.thumbnailImageView.image = myImage
当我尝试使用 Chrome 的 Postman 执行相同的请求时,我得到的图像没有任何问题。
有什么建议吗?谢谢
更新:我找到了一种与我想要的相去甚远但有效的方法
Alamofire.request(.GET, foods[indexPath.row].image, headers: headers)
.response { (request, response, data, error) in
debugPrint(response)
cell.thumbnailImageView.image = UIImage(data: data!, scale: 1)
}
这是香草 Alamofire,所以我仍然无法使用 AlamofireImage,它为图像提供了更好的支持。我从以下 post 得到这个方法: How to load image in swift using Alamofire (skywinder 的回答)
您输入的内容类型有误。应该是 image/jpeg
(http://www.sitepoint.com/web-foundations/mime-types-complete-list/).
let headers = ["Authorization": requestToken, "Content-Type": "image/jpeg"]
我希望这能解决问题。
所以我注意到服务器的答案是 "Content-Type" = "image/jpg"; 但正如用户@Gomines 指出的那样,正确的内容类型为alamofireImage 实际上是 "Content-Type": "image/jpeg"
然后我回到 AlamofireImages 文档并阅读了这篇文章
If the image you are attempting to download is an invalid MIME type not in the list, you can add custom acceptable content types using the addAcceptableImageContentTypes extension on the Request type.
所以我只需要在 AlamofireImage 请求之前添加这行代码:
Request.addAcceptableImageContentTypes(["image/jpg"])
问题是我没有匹配服务器响应的相同内容类型。但是,该行解决了问题,因为现在 AlamofireImage 也将接受服务器发送的相同类型。