26

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) {
    addAuthorizationHeader()
    Alamofire.request(.POST,createUrl(url), parameters: model, headers: headers,encoding:.Json)
}
Mario Dennis
  • 2,986
  • 13
  • 35
  • 50
  • Did the answer below solve your issue? You should mark it as accepted or provide additional info in order to solve your problem. – Majster May 16 '16 at 10:29

3 Answers3

35

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: "https://something.com")!
Alamofire.request(.POST, url, parameters: parameters, headers: headers, encoding: .URLEncodedInURL).response { request, response, data, error in
    print(request)
    print(response)
    print(data)
    print(error)
}

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.

Majster
  • 3,611
  • 5
  • 38
  • 60
  • 1
    For the form data to be in the body, one should remove the `encoding` parameter – Vince Sep 30 '16 at 14:01
  • If you instruct Alamofire to include the parameters into the query component of the URL you shouldn't set a content type at all. Otherwise, if you instruct Alamofire to put the parameters into the body, it will automatically add a content type header `application/x-www-form-urlencoded` for you - but also erroneously adds a `charset` parameter which is not defined for this MIME type. – CouchDeveloper Jun 12 '17 at 13:14
15

Alamofire 4.7.3 and Swift 4.0 above

As per the documentation for POST Request With URL-Encoded Parameters

let parameters: Parameters = [
    "foo": "bar", 
    "val": 1 
]

// All three of these calls are equivalent
Alamofire.request("https://httpbin.org/post", method: .post, parameters: parameters)
Alamofire.request("https://httpbin.org/post", method: .post, parameters: parameters, encoding: URLEncoding.default)
Alamofire.request("https://httpbin.org/post", method: .post, parameters: parameters, encoding: URLEncoding.httpBody)

// HTTP body: foo=bar&val=1

Alamofire 5.2

let parameters: [String: [String]] = [
    "foo": ["bar"],
    "baz": ["a", "b"],
    "qux": ["x", "y", "z"]
]

// All three of these calls are equivalent
AF.request("https://httpbin.org/post", method: .post, parameters: parameters)
AF.request("https://httpbin.org/post", method: .post, parameters: parameters, encoder: URLEncodedFormParameterEncoder.default)
AF.request("https://httpbin.org/post", method: .post, parameters: parameters, encoder: URLEncodedFormParameterEncoder(destination: .httpBody))

// HTTP body: "qux[]=x&qux[]=y&qux[]=z&baz[]=a&baz[]=b&foo[]=bar"
Suhit Patil
  • 11,748
  • 3
  • 50
  • 60
  • what does it mean by encoding anyway – YodagamaHeshan Jul 02 '20 at 13:17
  • 1
    @Yodagama When you pass information through a URL, you need to make sure it only uses specific allowed characters. These allowed characters include alphabetic characters, numerals, and a few special characters that have meaning in the URL string. Any other characters that need to be added to a URL should be encoded so that they don't cause the problem. To encode a URL, you simply replace the special characters with their encoding string eg space is replaced with %20 – Suhit Patil Jul 02 '20 at 19:48
  • 1
    https://github.com/Alamofire/Alamofire/blob/master/Documentation/Usage.md#post-request-with-url-encoded-parameters – Suhit Patil Jul 02 '20 at 19:48
  • does it mean the way of the request ? cause when i need to send data as query i select "URLEncoding.queryString"..... when i need to send data in body i select "URLEncoding.default" ...... is that mean the encoding change the request and above thing which you have mention in above comment ? – YodagamaHeshan Jul 03 '20 at 04:14
  • 1
    for GET requests `URLEncoding.queryString` or `URLEncoding.default` will appends an encoded string to the query of Request URL like `https://httpbin.org/get?foo=bar` and for POST requests `URLEncoding.default` or `URLEncoding.httpBody` will append encoded string to `URLRequest body` Check Alamofire documentation it has all the information. – Suhit Patil Jul 03 '20 at 06:33
12

Here is example code that should work with Alamofire 4.x and Swift 3.x as of August 2017:

let parameters = [
  "myParameter": "value"
]
Alamofire.request("https://something.com", method: .post, parameters: parameters, encoding: URLEncoding()).response { response in
  print(response.request)
  print(response.response)
  print(response.data)
  print(response.error)
}

There is no need to set the content-type header explicitly, as it is set by Alamofire automatically.

Max Desiatov
  • 5,087
  • 3
  • 48
  • 56