How to send a POST request with BODY in swift
You're close. The parameters dictionary formatting doesn't look correct. You should try the following:
let parameters: [String: AnyObject] = [
"IdQuiz" : 102,
"IdUser" : "iosclient",
"User" : "iosclient",
"List": [
[
"IdQuestion" : 5,
"IdProposition": 2,
"Time" : 32
],
[
"IdQuestion" : 4,
"IdProposition": 3,
"Time" : 9
]
]
]
Alamofire.request(.POST, "http://myserver.com", parameters: parameters, encoding: .JSON)
.responseJSON { request, response, JSON, error in
print(response)
print(JSON)
print(error)
}
Hopefully that fixed your issue. If it doesn't, please reply and I'll adjust my answer accordingly.
Alamofire post request:
There are many ways to implement the requests using Alamofire, this is a simple example:
First, do you have to create the parameters, URL from your API and headers:
let parameters = [
"username": "foo",
"password": "123456"
]
let url = "https://httpbin.org/post"
static private var headers: HTTPHeaders {
get {
return [
"Authorization" : "Bearer \(Session.current.bearerToken ?? "")"
]
}
}
So you call the function from Alamofire and pass your data:
Alamofire.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers).responseJSON {
response in
switch (response.result) {
case .success:
print(response)
break
case .failure:
print(Error.self)
}
}
}
:)
How Make Request Body for Put Request in Swift
For this endpoint it would be
let parameters: [String: Any] = [
"context_uri": "spotify:album:5ht7ItJgpBH7W6vJ5BqpPr",
"offset": [
"position": 5
],
"position_ms": 0
]
And make sure to use JSON encoding.
POST request with data in body with Alamofire 4
You need to send request like below in swift 3
let urlString = "https://httpbin.org/get"
Alamofire.request(urlString, method: .post, parameters: ["foo": "bar"],encoding: JSONEncoding.default, headers: nil).responseJSON {
response in
switch response.result {
case .success:
print(response)
break
case .failure(let error):
print(error)
}
}
Swift 5 with Alamofire 5:
AF.request(URL.init(string: url)!, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers).responseJSON { (response) in
print(response.result)
switch response.result {
case .success(_):
if let json = response.value
{
successHandler((json as! [String:AnyObject]))
}
break
case .failure(let error):
failureHandler([error as Error])
break
}
}
Alamofire set request body with Data
Alamfire accepts [String:Any]
do {
let params = try JSONSerialization.jsonObject(with: data, options: []) as! [String:Any]
Alamofire.request(url, method: .post, parameters:params, encoding: JSONEncoding.default, headers: headers)
request.validate().responseJSON {
...
}
}
catch {
print(error)
}
Swift 4.2
Alamofire.request(url, method: .post, parameters: [:], encoding: "test", headers: [:])
extension String: ParameterEncoding {
public func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest {
var request = try urlRequest.asURLRequest()
request.httpBody = data(using: .utf8, allowLossyConversion: false)
return request
}
}
Related Topics
How to Convert from Degrees to Radians
Ios App With Arc, Find Who Is Owner of an Object
Stringbyappendingpathcomponent Is Unavailable
Firebase Notification Records/Log API
Xcode 6: Keyboard Does Not Show Up in Simulator
Uibutton Custom Font Vertical Alignment
The Model Used to Open the Store Is Incompatible With the One Used to Create the Store
How to Customize the Background Color of a Uitableviewcell
How to Write Text on Image in Objective-C (Ios)
When to Use Dequeuereusablecellwithidentifier VS Dequeuereusablecellwithidentifier: Forindexpath
Uitapgesturerecognizer Breaks Uitableview Didselectrowatindexpath
Dealing With Different iOS Device Resolutions in Spritekit
Do Something Every X Minutes in Swift
Swift Saving and Retrieving Custom Object from Userdefaults
Determine on Iphone If User Has Enabled Push Notifications