3

I am facing an issue with AFNetworking 2.0 (coding on Swift). GET method, of library seems that works perfect for me. Although, POST method returns me error. The error is Bad username/password. From the first looking that seem an authentication issue, but actually when I tried the POST request on Postman (REST Client) worked perfect.

So what I am doing:

Configuration:

var policy : AFSecurityPolicy = AFSecurityPolicy();
policy.allowInvalidCertificates = true;

self.client = AFHTTPRequestOperationManager(baseURL: self.basicUrl)
self.client!.operationQueue = NSOperationQueue.mainQueue()
self.client!.securityPolicy = policy

var responseSerializer : AFJSONResponseSerializer = AFJSONResponseSerializer()

self.client!.responseSerializer = responseSerializer

var requestSerializer : AFJSONRequestSerializer = AFJSONRequestSerializer()
requestSerializer.setValue("application/json", forHTTPHeaderField: "Accept")

self.client!.requestSerializer = requestSerializer

and my request

internal func loginUser(email : String, password : String, onCompletion : WebClientLoginResponse) -> Void
{
    var params : Dictionary = ["email":email, "pass":password]

    self.client!.POST("login", parameters: params, success: { (operation : AFHTTPRequestOperation!, response : AnyObject!) -> Void in

        var finalResponse : Dictionary = Dictionary<String, String>()

        finalResponse = response as Dictionary

        onCompletion(true, nil, nil, nil)

        }) { (operation : AFHTTPRequestOperation!, error : NSError!) -> Void in

            println("Error \(error.description) \(operation.responseObject) ")

            onCompletion(false, nil, nil, nil)
    }
}

What do you think guys? Am I missing anything?

Thank you!

Tzegenos
  • 837
  • 12
  • 16

1 Answers1

2

Does your server require JSON-encoded parameters? See the accepted answer to this question.

Also, consider using the Swift version of AFNetworking, AlamoFire, which has an easier (IMHO) way to set the encoding format.

Community
  • 1
  • 1
Todd Agulnick
  • 1,945
  • 11
  • 10
  • 1
    Hey Todd thanks for your answer first of all. It seems that the problem was that I was setting requestSerializer as well. (not only responseSerializer). It seems that if you are assigning both of them, POST does not working (for some strange reason). As a result, when I removed the lines, where I was configuring requestSerializer, POST was working properly! I don't know why! – Tzegenos Oct 30 '14 at 23:54
  • 2
    Oh, I didn't notice that! Seems like maybe a bug in AFNetworking. Glad it's working now. – Todd Agulnick Oct 31 '14 at 00:05
  • 1
    @Tzegenos Thanks, Removing these lines solved the issue for me to! – NBoymanns Sep 08 '15 at 14:49