post application/x-www-form-urlencoded Alamofire

26,668

Solution 1

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.

Solution 2

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"

Solution 3

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.

Share:
26,668

Related videos on Youtube

Mario Dennis
Author by

Mario Dennis

Updated on July 09, 2022

Comments

  • Mario Dennis
    Mario Dennis almost 2 years

    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)
    }
    
    • Majster
      Majster almost 8 years
      Did the answer below solve your issue? You should mark it as accepted or provide additional info in order to solve your problem.
  • Vince
    Vince over 7 years
    For the form data to be in the body, one should remove the encoding parameter
  • CouchDeveloper
    CouchDeveloper almost 7 years
    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.
  • Yodagama
    Yodagama almost 4 years
    what does it mean by encoding anyway
  • Suhit Patil
    Suhit Patil almost 4 years
    @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
    Suhit Patil almost 4 years
  • Yodagama
    Yodagama almost 4 years
    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 ?
  • Suhit Patil
    Suhit Patil almost 4 years
    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.