Can anyone give me example on AFNetworking 3.0 in post method?

2.6k Views Asked by At

I am struck on it because there is no AFHTTPRequestoperation to find difficult on it. please use on afnetworking 3.0 in swift.

2

There are 2 best solutions below

0
On

Just use Alamofire if you need to implement it on swift. Check answer here which shows example of Alamofire post method.

0
On

AFHTTPRequestoperation class removed in Afnetworking 3.0

https://github.com/AFNetworking/AFNetworking/wiki/AFNetworking-3.0-Migration-Guide

Try this:

func PostData(){

    let parameters : NSMutableDictionary? = [

        "UserID": String(300),
        "UserProfileID": String(356)]

        let manager = AFHTTPSessionManager()
        let serializerRequest = AFJSONRequestSerializer()
        serializerRequest.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
        manager.requestSerializer = serializerRequest

        let serializerResponse = AFJSONResponseSerializer()
        serializerResponse.readingOptions = JSONSerialization.ReadingOptions.allowFragments
        serializerResponse.acceptableContentTypes = ((((NSSet(object: "application/json") as! Set<String>) as Set<String>) as Set<String>) as Set<String>) as Set<String>;
        manager.responseSerializer = serializerResponse
        manager.post(Webserive.DefaultProfile, parameters: parameters, progress: nil, success: { (task: URLSessionDataTask, responseObject: Any?) in
        if (responseObject as? [String: AnyObject]) != nil {

             print("responseObject \(responseObject)")
        }
        }) { (task: URLSessionDataTask?, error: Error) in
            print("POST fails with error \(error)")
       }
}