Reputation: 31
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":"[email protected]","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 Response:
{
"success": true,
"message": "Advertisement Posted Successfully!"
}
Model Class:
import Foundation
// MARK: - CommonData
struct CommonData: Codable {
let success: Bool
let message: String
}
ERROR:
failure(Alamofire.AFError.responseSerializationFailed(reason: Alamofire.AFError.ResponseSerializationFailureReason.decodingFailed(error: Swift.DecodingError.typeMismatch(Swift.String, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "message", intValue: nil)], debugDescription: "Expected to decode String but found a dictionary instead.", underlyingError: nil)))))
Try to post API call.
Upvotes: 0
Views: 258
Reputation: 36782
When you have a successfull POST
, the response is CommonData
as you have it.
However, when there is some error, then you get CommonData2
(see below).
For example in your params
, a "location": ""
is required.
Similarly with "max_transaction_limit": "2"
, note it is > 1.
In those cases you have to decode CommonData2
.
Same when you use let params = ["email":"[email protected]","password":"Test@123"]
, you get
CommonData2
response.
struct CommonData2: Codable {
let success: Bool
let result: String?
let message: Message
}
struct Message: Codable {
let errors: [String: [String]]
}
To decode both types of responses, you could try this approach, using init(from decoder: Decoder)
,
works for me:
struct CommonData2: Codable {
let success: Bool
let result: String?
let message: Message?
var msg: String?
enum CodingKeys: String, CodingKey {
case success, message, result
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.result = try? container.decode(String.self, forKey: .result)
self.success = try container.decode(Bool.self, forKey: .success)
if let theMessage = try? container.decode(Message.self, forKey: .message) {
self.message = theMessage
} else {
self.message = nil
}
if let theString = try? container.decode(String.self, forKey: .message) {
self.msg = theString
} else {
self.msg = nil
}
}
}
Upvotes: 0