0
votes

I'm trying to make a request to my API on an app in swift using AlamoFire. I need to use so custom headers so I opted to use the NSURLRequest approach, here is my code:

let URL = NSURL(string: "\(APIUrl)/sessions")!
let mutableURLRequest = NSMutableURLRequest(URL: URL)
mutableURLRequest.HTTPMethod = "POST"

let parameters = ["oauth_token": OAuthToken]
var JSONSerializationError: NSError? = nil

mutableURLRequest.HTTPBody = NSJSONSerialization.dataWithJSONObject(parameters, options: nil, error: &JSONSerializationError)
mutableURLRequest.addHeadersInDictionary(self.defaultHeaders)

Alamofire.request(mutableURLRequest)
         .validate(statusCode: 200..<300)
         .responseJSON { [weak self] request, response, JSON, error in
             println(request.description)
             if let strongSelf = self, response = response {
                 if error == nil {
                     let jsonResult = JSON as! Dictionary<String, AnyObject>
                     strongSelf.setAuthToken(jsonResult[JsonConstants.AuthToken] as! String)
                     strongSelf.defaultHeaders["Authorization"] = "Token token=\(strongSelf.authToken()!)"
                     success?()
                 } else {
                     println("Failed with \(response.statusCode)")
                     println(error!)
                     failure?()
                 }
             } else  {
                 println(error!)
                 failure?()
             }
     }

I receive the request perfectly on my API, but on my logs I see that the parameters sent where not exactly the ones I sent:

Parameters: {"oauth_token"=>"token", "session"=>{"oauth_token"=>"token"}}

Is there any reason why AlamoFire is adding a session : [] parameter to my HTTP Body? and is there a way to clean those parameters just to send the required information to the server, or at least remove the oauth_token that's outside the session parameter?

Also if I sent the request without adding the HTTPBody I will receive on my server just the session parameter empty:

Parameters: {"session"=>{}}

Thanks in advance!

1

1 Answers

0
votes

That session parameter is not being appended by Alamofire. You must be adding it somewhere in your logic that is not shown in your example. Additionally, what does your addHeadersInDictionary method doing?