POST request with a simple string in body with Alamofire


You can do this:

  1. I created a separated request Alamofire object.
  2. Convert string to Data
  3. Put in httpBody the data

    var request = URLRequest(url: URL(string: url)!)
    request.httpMethod =
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")
    let pjson = attendences.toJSONString(prettyPrint: false)
    let data = (pjson?.data(using: .utf8))! as Data
    request.httpBody = data
    Alamofire.request(request).responseJSON { (response) in

how is it possible to send a POST request with a simple string in the HTTP body with Alamofire in my iOS app?

As default Alamofire needs parameters for a request:

Alamofire.request(.POST, "", parameters: ["foo": "bar"])

These parameters contain key-value-pairs. But I don't want to send a request with a key-value string in the HTTP body.

I mean something like this:

Alamofire.request(.POST, "", body: "myBodyString")

If you are using Swift 3 and Alamofire v4.0 then the accepted answer would look like this:

let parameters: [String: Any] = [
    "IdQuiz" : 102,
    "IdUser" : "iosclient",
    "User" : "iosclient",
    "List": [
            "IdQuestion" : 5,
            "IdProposition": 2,
            "Time" : 32
            "IdQuestion" : 4,
            "IdProposition": 3,
            "Time" : 9

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

Alamofire 4 Swift 3 ParameterEncoding Custom

In Alamofire 4.0 you should use ParameterEncoding protocol. Here is an example, which makes any String UTF8 encodable.

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


Alamofire.request("", method: .post, parameters: [:], encoding: "myBody", headers: [:])

How can i use alamofire for post api

First of all you add almofire library into your project then import almofire into your ViewController then below method apply in your button action.

func webServiceLogin(isFbLogin:Bool,email:String,password:String)
            var parameters:[String:String]?

             parameters = ["hash":email as String,"key":password ]

            Alamofire.request("your url", method: .post, parameters: parameters,encoding: URLEncoding.default, headers: nil).responseJSON {
                response in
                switch response.result {
                case .success:
                    if let dictSuccess:NSDictionary =  response.value as! NSDictionary?


                case .failure(let error):
                    Alert.showAlertWithTitle(strTitle: appTitle, strMessage: error.localizedDescription, onView: self)


You can use alamofire manager

    var alamoFireManager = Alamofire.SessionManager
    let request = URLRequest(url:_yourULR)
    request.HTTPMethod = requestMethod.rawValue
    request.timeoutInterval = //set yours
    request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
    request.addValue("application/json", forHTTPHeaderField: "Accept")
    request.addValue("gzip", forHTTPHeaderField: "Accept-Encoding")

    request.HTTPBody = "you_bodydataSTring".dataUsingEncoding(String.Ecoding.utf8)

        .responseString { (response) -> Void in
            let datastring = NSString(!, encoding: String.Ecoding.utf8)
            switch response.result {
            case .Success:

                if response.response?.statusCode == 200 {
                   //code for success

            case .Failure(let error):
               //request failed