0

In objC I do:

NSData *postData = ...
NSMutableURLRequest *request = ...
[request setHTTPBody:postData];

How to do in Swift Alamofire?

Alamofire.request(url, method: .post, parameters: nil, encoding: URLEncoding.default, headers: headers)
    request.validate().responseJSON {
...
}
ArisRS
  • 1,362
  • 2
  • 19
  • 41
  • set `parameters` ["some key" : "some data"] and `encoding:` `JSONEncoding.default` – SPatel Dec 24 '18 at 13:06
  • It depends upon the nature of the `postData`, because generally you would rather just supply Alamofire the `Dictionary` and it will prepare the body of the request for you. If you absolutely must build the request, you actually can send that directly with Alamofire (e.g. https://stackoverflow.com/q/28016578/1271826), but in that case, you’d set the URL and method as part of the `URLRequest`, not as parameters to this method. And you have to be very careful with headers and the like. Perhaps you can tell us what the `postData` looks like and we can advise further. – Rob Dec 24 '18 at 14:45
  • Actually it is a zip archive. Looks like below Sh_Khan comment can help. – ArisRS Dec 24 '18 at 15:29

2 Answers2

2

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
    }

}
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • let data = "test".data(using: .utf8) let params = try? JSONSerialization.jsonObject(with: data, options: []) as! [String: Any] params is nil?? – ArisRS Dec 24 '18 at 13:18
  • because test is a string and it's data can't be casted to `[String:Any]` , can you make it like `["someKey":"test"]`? – Shehata Gamal Dec 24 '18 at 13:22
  • In this case I can set parameters: as Dictionary without try JSONSerialization etc., but I need something the same as in objective C: [request setHTTPBody:postData]; I don't know how to do this. – ArisRS Dec 24 '18 at 13:24
0

Use Alamofire AND Swifty Json Library for suitable api call and json parsing

Swifty Json https://github.com/SwiftyJSON/SwiftyJSON

Alamofire https://github.com/Alamofire/Alamofire

First create your parameter list in [String: Any] type

func CallAPI(){

    let parameters: [String: Any] = [
    "Username": "Admin",
    "Password": "123456",
    "Language_Code": "EN"]


    Alamofire.request("Your API Url", method: .post, parameters: parameters, encoding: JSONEncoding.default)
    .responseJSON { response in

    if((response.result.value) != nil) {

    let ResultJson = JSON(response.result.value!)

    print("ResultJson==",ResultJson)
    }
 }
}
Onkar Borse
  • 70
  • 1
  • 7