我有参数:
{"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": ""}
我把它放在体内了
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
}
我称它为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)
当我使用Alamofire进行调用时,它总是返回nil,因此算作失败:
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
}
}
标头仅是Content-Type:application / json
使它起作用的正确代码是什么?我已经放置了plist例外域,并允许人为负载。
答案 0 :(得分:1)
像这样正确设置请求正文和标头:
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")
答案 1 :(得分:0)
let param:[String:Any] = [“” key“:” Value“]
await
这将为您提供正确的json转换响应。 再三考虑,请从API方面确认所有参数和标头类型。