Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

POST request with data in body with Alamofire 4

how is it possible to send a POST request with a data in the HTTP body with Alamofire 4? I used custom encoding at swift 2.3 it was working good. I converted my code swift 3 and I tried to paramater encoding but not working. This code :

public struct MyCustomEncoding : ParameterEncoding { private let data: Data init(data: Data) {     self.data = data } public func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest {      var urlRequest = try urlRequest.asURLRequest()             do {                         urlRequest.httpBody = data             urlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")      } catch {         throw AFError.parameterEncodingFailed(reason: .jsonEncodingFailed(error: error))     }      return urlRequest } 

and Alamofire request :

let enco : ParameterEncoding = MyCustomEncoding(data: ajsonData)     Alamofire.request(urlString, method: .post , parameters: [:], encoding: enco , headers: headers).validate()                 .responseJSON { response in                     switch response.result {                     case .success:                         print(response)                          break                     case .failure(let error):                          print(error)                     }     } 
like image 534
Cagatay Avatar asked Nov 15 '16 07:11

Cagatay


People also ask

How do I pass Alamofire header in Swift?

For headers that change from request to request, you can pass them directly to the request method. From the docs: Adding a custom HTTP header to a Request is supported directly in the global request method. This makes it easy to attach HTTP headers to a Request that can be constantly changing.


2 Answers

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         }     } 
like image 109
Ekta Padaliya Avatar answered Sep 25 '22 23:09

Ekta Padaliya


Alamofire using post method import UIKit import Alamofire

class ViewController: UIViewController {     let parameters = [         "username": "foo",         "password": "123456"     ]     let url = "https://httpbin.org/post"  override func viewDidLoad() {         super.viewDidLoad() Alamofire.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: [:]).responseJSON {             response in             switch (response.result) {             case .success:                 print(response)                 break             case .failure:                 print(Error.self)             }         } } 
like image 26
Srinivasan_iOS Avatar answered Sep 23 '22 23:09

Srinivasan_iOS