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.
Related videos on Youtube
Author by
Mario Dennis
Updated on July 09, 2022Comments
-
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 almost 8 yearsDid the answer below solve your issue? You should mark it as accepted or provide additional info in order to solve your problem.
-
-
Vince over 7 yearsFor the form data to be in the body, one should remove the
encoding
parameter -
CouchDeveloper almost 7 yearsIf 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 acharset
parameter which is not defined for this MIME type. -
Yodagama almost 4 yearswhat does it mean by encoding anyway
-
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 almost 4 years
-
Yodagama almost 4 yearsdoes 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 almost 4 yearsfor GET requests
URLEncoding.queryString
orURLEncoding.default
will appends an encoded string to the query of Request URL likehttps://httpbin.org/get?foo=bar
and for POST requestsURLEncoding.default
orURLEncoding.httpBody
will append encoded string toURLRequest body
Check Alamofire documentation it has all the information.