gpt4 book ai didi

swift - Alamofire 3.2 : How do I validate the response of an 'upload' POST call?

转载 作者:搜寻专家 更新时间:2023-10-31 08:18:14 27 4
gpt4 key购买 nike

我有一个简单的上传 POST 调用,

Alamofire.upload(
.POST,
"https://httpbin.org/post",
multipartFormData: { multipartFormData in
multipartFormData.appendBodyPart(fileURL: unicornImageURL, name: "unicorn")
multipartFormData.appendBodyPart(fileURL: rainbowImageURL, name: "rainbow")
},
encodingCompletion: { encodingResult in
switch encodingResult {
case .Success(let upload, _, _):
upload.responseJSON { response in
debugPrint(response)
}
case .Failure(let encodingError):
print(encodingError)
}
}
)

即使响应是 404/500 错误,它也总是转到 .Success 案例。

我的问题是,如何验证此请求的响应?

最佳答案

验证

默认情况下,无论响应内容如何,​​Alamofire 都会将任何已完成的请求视为成功。如果响应具有 Not Acceptable 状态代码或 MIME 类型,则在响应处理程序之前调用验证会导致生成错误。手动验证

Alamofire.request(.GET, "https://httpbin.org/get", parameters: ["foo": "bar"])
.validate(statusCode: 200..<300)
.validate(contentType: ["application/json"])
.response { response in
print(response)
}

自动验证

自动验证 200...299 范围内的状态代码,以及响应的 Content-Type header 是否与请求的 Accept header 匹配(如果提供的话)。

Alamofire.request(.GET, "https://httpbin.org/get", parameters: ["foo": "bar"])
.validate()
.responseJSON { response in
switch response.result {
case .Success:
print("Validation Successful")
case .Failure(let error):
print(error)
}
}

好吧,终于

case .Success(let upload, _, _):
upload.validate(statusCode: 200..<300)
.validate(contentType: ["application/json"])
.response { response in
print(response)
}

关于swift - Alamofire 3.2 : How do I validate the response of an 'upload' POST call?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35425507/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com