Mario Dennis Mario Dennis - 6 months ago 321
iOS Question

post application/x-www-form-urlencoded Alamofire

I want to use Alamofire to retrieve a bearer token from Web API but I am new to ios and alamofire. How can I accomplish this with Alamofire?

func executeURLEncodedRequest(url: URL, model: [String : String]?, handler: RequestHandlerProtocol) {
Alamofire.request(.POST,createUrl(url), parameters: model, headers: headers,encoding:.Json)


Well you don't really need Alamofire to do this (it can be simply done using a plain NSURLRequest) but here goes:

let headers = [
    "Content-Type": "application/x-www-form-urlencoded"
let parameters = [
    "myParameter": "value"
let url = NSURL(string: "")!
Alamofire.request(.POST, url, parameters: parameters, headers: headers, encoding: .URLEncodedInURL).response { request, response, data, error in

I think that the headers can be omitted since alamofire will append the appropriate Content-Type header. Let me know if it works.

You can also find a ton of specification with examples here.