Reputation: 13
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!
Upvotes: 0
Views: 888
Reputation: 625
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
Upvotes: 1
Reputation: 5523
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)
}
}
}
Upvotes: 0