func postAd(cryptoo: String, fiatt: String,locationn: String,countryy: String,marginn: String,
pricee: String,minlimitt: String,maxlimitt: String,paymrntwindoww: String,paymrntmethodd: String,
accountdetailss: String,infoo: String,termss: String
){
let endpoint = "https://new.demozab.com/armup/api/public/api/post-trade"
// let params = ["email":"smith@mailinator.com","password":"Test@123"]
let params = [
"crypto_currency_id":"1",
"trade_type": "buy",
"location": "",
"country": "1",
"market":"15",
"margin":"1",
"trade_price":"1",
"min_transaction_limit":"1",
"max_transaction_limit":"1",
"payment_method":"1",
"payment_window":"1",
"account_details": "1",
"additional_information":"1",
"sunday_start":"",
"sunday_end":"",
"monday_start":"",
"monday_end":"",
"tuesday_start":"",
"tuesday_end":"",
"wednesday_start":"",
"wednesday_end":"",
"thursday_start":"",
"thursday_end":"",
"friday_start":"",
"friday_end": "",
"saturday_start": "",
"saturday_end": "",
"terms":"i",
]
guard let url = URL(string: endpoint)
else {
print("Invalid URL")
return
}
let headers: HTTPHeaders = [
"Authorization": "Bearer TVfXc1gPZbLfruJ53Q7PkkSDjSXi6gXGWXkCXg6j",
"Content-Type": "application/json"
]
AF.request( url, method: .post, parameters: params, headers: headers ).responseDecodable(of: CommonData.self) {
(response) in
print(response.result)
/////////
}
}
API回复:
{“成功”:真,“消息”:“广告发布成功!”}
型号分类:
import Foundation
// MARK: - CommonData
struct CommonData: Codable {
let success: Bool
let message: String
}
'
错误:
failure(Alamofire.AFError.responseSerializationFailed(reason:Alamofire.AFError.ResponseSerializationFailureReason.decodingFailed(错误:Swift.解码错误.类型不匹配(Swift.字符串,Swift.解码错误.上下文(编码路径:[编码键(字符串值:“message”,intValue:nil)],调试说明:“应解码字符串,但找到的却是字典。",underlyingError:无))))))
尝试发布API调用
1条答案
按热度按时间yacmzcpb1#
当你有一个成功的
POST
时,响应是CommonData
。但是,当出现一些错误时,则会得到
CommonData2
(见下文)。例如,在
params
中,需要"location": ""
。与"max_transaction_limit": "2"
类似,注意它〉1。在这些情况下,您必须解码CommonData2
。当你使用
let params = ["email":"smith@mailinator.com","password":"Test@123"]
时,你会得到CommonData2
的响应。要解码这两种类型的响应,您可以尝试使用
init(from decoder: Decoder)
的方法,对我来说很有效: