I want to make a POST request use Alamofire 4.0,And,I hope that my request parameter is transmitted to the server in the form of text rather than json. Please help me? Thanks!
Asked
Active
Viewed 883 times
2 Answers
1
you should use .Custom for encoding parameter and create your own custom encoding and add it to your HTTP body in earlier alamofire , but in Alamafire 4.0 you should extend the ParameterEncoding
extension String: ParameterEncoding {
public func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest {
var request = try urlRequest.asURLRequest()
request.httpBody = data(using: .utf8, allowLossyConversion: false)
return request
}
}
you can find example code here -> stack overflow thread

Community
- 1
- 1

Maryam Fekri
- 605
- 8
- 19
-
I said parameter is HTTPBody not response – Gigi Dec 21 '16 at 06:51
0
Try Like this
func API_getSubject(id:String) {
MBProgressHUD.showAdded(to: self.view, animated: true)
let param = ["idnumber" : id];
print("param == \(param)")
Alamofire.request(Config.BASE_URL + "subject_list", method: .post, parameters: param, encoding: JSONEncoding.default, headers: nil)
.responseJSON {
response in debugPrint(response) // prints detailed description of all response properties
//to get JSON return value
if let result = response.result.value {
let JSON = result as! NSDictionary
if JSON.value(forKey: "status") as! NSNumber == 1 {
self.ary_responce = (JSON["subjects"]! as! NSArray).mutableCopy() as! NSMutableArray
self.aTable.reloadData()
}
else{
self.Alert(msg: "\(JSON["message"]!)")
}
}
else{
if let error = response.result.error {
self.Alert(msg: error.localizedDescription)
}
}
DispatchQueue.main.async {
MBProgressHUD.hideAllHUDs(for: self.view, animated: true)
MBProgressHUD.hide(for: self.view, animated: true)
}
}
}

Museer Ahamad Ansari
- 5,414
- 3
- 39
- 45