Reputation: 717
I have parameter :
{"name": "oaseme dabo", "spouseName": "", "fatherName": "hajime kasuga", "income": "Please Choose", "homeAddress": "", "locationAccessibility": "Please Choose", "gender": "Please Choose", "exactOccupation": "", "maritalStatus": "Please Choose", "source_id": "123456", "dateOfBirth": "01 January 1900", "exactIncome": "1", "howInfluenceThisPerson": "Please Choose", "motherName": "shizuka kasuga", "knowThisPerson": "Please Choose", "prospectingStatus": "Please Choose", "ageBand": "Choose Age Band", "occupation": "Please Choose", "sourceOfProspect": "Family & Relatives", "agentCode": "123456", "totalChildren": "0", "howCloseAreYou": "Please Choose", "workAddress": "", "exactSourceOfProspect": ""}
I put it into body :
class func sentContactBody(agentCode: String, source_id: String, dateOfBirth: String, ageBand: String, exactIncome: String, exactOccupation: String, fatherName: String, gender: String, homeAddress: String, howCloseAreYou: String, howInfluenceThisPerson: String, income: String, knowThisPerson: String, locationAccessibility: String, maritalStatus: String, motherName: String, name: String, occupation: String, prospectingStatus: String, sourceOfProspect: String, spouseName: String, totalChildren: String, workAddress: String, exactSourceOfProspect: String) -> Parameters{
let sentContact : Parameters = [
"agentCode": agentCode,
"source_id": source_id,
"dateOfBirth": dateOfBirth,
"ageBand": ageBand,
"exactIncome": exactIncome,
"exactOccupation": exactOccupation,
"fatherName": fatherName,
"gender": gender,
"homeAddress": homeAddress,
"howCloseAreYou": howCloseAreYou,
"howInfluenceThisPerson": howInfluenceThisPerson,
"income": income,
"knowThisPerson": knowThisPerson,
"locationAccessibility": locationAccessibility,
"maritalStatus": maritalStatus,
"motherName": motherName,
"name": name,
"occupation": occupation,
"prospectingStatus": prospectingStatus,
"sourceOfProspect": sourceOfProspect,
"spouseName": spouseName,
"totalChildren": totalChildren,
"workAddress": workAddress,
"exactSourceOfProspect": exactSourceOfProspect
]
return sentContact
}
and I called it as param :
let param = HTTPBody.sentContactBody(agentCode: agentCode, source_id: source_id, dateOfBirth: dateOfBirth, ageBand: ageBand, exactIncome: exactIncome, exactOccupation: exactOccupation, fatherName: fatherName, gender: gender, homeAddress: homeAddress, howCloseAreYou: howCloseAreYou, howInfluenceThisPerson: howInfluenceThisPerson, income: income, knowThisPerson: knowThisPerson, locationAccessibility: locationAccessibility, maritalStatus: maritalStatus, motherName: motherName, name: name, occupation: occupation, prospectingStatus: prospectingStatus, sourceOfProspect: sourceOfProspect, spouseName: spouseName, totalChildren: totalChildren, workAddress: workAddress, exactSourceOfProspect: exactSourceOfProspect)
when I used Alamofire to called, it always return nil so it count as failure:
Alamofire.request(Constant.sentContactSAMDB(), method: .post, parameters: param, encoding: JSONEncoding.default, headers: ConnectionManager.sharedIns.getHTTPHeadersSAMDB())
.responseObject{ (response: DataResponse<SAMDBModel>) in
switch(response.result) {
case .success(_):
print("contact response result value:\(response.result.value)")
if response.result.value != nil {
self.samDBModel = response.result.value
let status = self.samDBModel?.status
let statusCode = response.response?.statusCode
print("sent contact statusCode:\(String(describing: statusCode))")
if(statusCode == 200){
let result = self.samDBModel?.result
let samDBStatus = result![0].status
let samDBError = result![0].error
print("sent contact samDBStatus:\(samDBStatus)")
}
}
break
case .failure(_):
print("contact failure response result value:\(response.result.value)")
self.samDBModel = response.result.value
let status = self.samDBModel?.status ?? "Failed connect to Tokio Marine server"
break
}
}
the headers is only Content-Type : application/json
What is the correct code to make it work? I already put plist exception domain and allow arbitary loads YES.
Upvotes: 1
Views: 1798
Reputation: 526
let param:[String:Any] = ["key":"Value"]
request(url, method: method, parameters: param, encoding: JSONEncoding.default, headers: yourheader).responseJSON { (response) in
if(response.result.isSuccess){
}
else{
}
}
This will give you proper json converted response. On second thought please confirm all parameter and header types from API side.
Upvotes: 0
Reputation: 1340
set request body and header properly like this:
let data = try! JSONSerialization.data(withJSONObject: your_parameters, options: JSONSerialization.WritingOptions.prettyPrinted)
var request = URLRequest(url: your_URL)
request.httpBody = data
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
Upvotes: 3