88

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, "http://mywebsite.com/post-request", 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, "http://mywebsite.com/post-request", body: "myBodyString")
Karl
  • 1,451
  • 3
  • 12
  • 13

12 Answers12

91

Your example Alamofire.request(.POST, "http://mywebsite.com/post-request", parameters: ["foo": "bar"]) already contains "foo=bar" string as its body. But if you really want string with custom format. You can do this:

Alamofire.request(.POST, "http://mywebsite.com/post-request", parameters: [:], encoding: .Custom({
            (convertible, params) in
            var mutableRequest = convertible.URLRequest.copy() as NSMutableURLRequest
            mutableRequest.HTTPBody = "myBodyString".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
            return (mutableRequest, nil)
        }))

Note: parameters should not be nil

UPDATE (Alamofire 4.0, Swift 3.0):

In Alamofire 4.0 API has changed. So for custom encoding we need value/object which conforms to ParameterEncoding protocol.

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("http://mywebsite.com/post-request", method: .post, parameters: [:], encoding: "myBody", headers: [:])
Silmaril
  • 4,153
  • 18
  • 22
  • This works perfectly, not only for simple but all kinds of JSON type mapped strings. – Sahil Kapoor May 18 '15 at 13:45
  • 1
    @silmaril in my situation I can get raw request from backend only if I use PUT method, could you please help me, why POST request can't see anything – A.J. Mar 10 '16 at 03:43
  • 1
    .Custom isn't available anymore in Alamofire 4 Swift 3, and I am not able to find the alternative, any hints? – Raheel Sadiq Sep 19 '16 at 11:42
  • @Silmaril I posted question too, please answer that so I can close it: http://stackoverflow.com/questions/39573514/alamofire-4-swift-3-parameterencoding-custom – Raheel Sadiq Sep 19 '16 at 12:49
  • @Silmaril does not work for me. Converts "myBody" to `{ myBody: '' }`. Swift 3. Alamofire 4.0.0. – Oleg Kubrakov Sep 27 '16 at 14:20
  • Can't tell you what are you doing wrong. I just double checked and it works. ```$ curl -i \ -X POST \ -H "Accept-Language: en;q=1.0" \ -H "User-Agent: TestCalendar/1.0 (com.test.alamofire; build:1; iOS 10.0.0) Alamofire/4.0.0" \ -H "Accept-Encoding: gzip;q=1.0, compress;q=0.5" \ -d "myBody" \ "http://mywebsite.com/post-request"``` – Silmaril Sep 27 '16 at 14:34
  • the encoding parameter is not supposed to be a String – quemeful Oct 27 '16 at 13:29
  • please double check my answer: there is an example of how string can become a valid encoding parameter with a help of extension – Silmaril Oct 27 '16 at 14:36
  • @Silmaril I have posted a question like this here if you can please share an answer http://stackoverflow.com/questions/40385992/how-to-send-a-xml-object-with-alamofire – mike vorisis Nov 02 '16 at 21:05
  • This is genius :) :) @quemeful the String is made to extend ParameterEncoding in the first pard of the code – Alex Feb 13 '17 at 16:10
  • Works perfectly. This should be marked as the correct answer. – Baylor Mitchell Apr 12 '17 at 23:21
  • with Alamofire 4.7.3, Swift 4.2 and Xcode 10 everything looks perfect – Andres Paladines Oct 29 '18 at 17:15
  • Can you tell how to use it with more detail? what to do with encode function? – Ahmadreza Jan 10 '19 at 16:27
  • Last line `Alamofire.request(....` shows the usage. `encode` method is called by *Alamofire* during request putting `String` value in request's body with a help `encode` method – Silmaril Jan 11 '19 at 10:59
  • This isn't posting using the body, it's posting params – froggomad Jun 25 '19 at 05:13
67

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 = HTTPMethod.post.rawValue
    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
    
    
        print(response)
    
    }
    
afrodev
  • 1,054
  • 9
  • 10
14

If you use Alamofire, it is enough to encoding type to URLEncoding.httpBody

With that, you can send your data as a string in the httpbody although you defined it json in your code.

It worked for me..

Updated for Badr Filali's question:

var url = "http://..."
let _headers : HTTPHeaders = ["Content-Type":"application/x-www-form-urlencoded"]
let params : Parameters = ["grant_type":"password","username":"mail","password":"pass"]

let url =  NSURL(string:"url" as String)

request(url, method: .post, parameters: params, encoding: URLEncoding.httpBody, headers: _headers).responseJSON(
    completionHandler: { response in response
        let jsonResponse = response.result.value as! NSDictionary
        
        if jsonResponse["access_token"] != nil
        {
            access_token = String(describing: jsonResponse["accesstoken"]!)
        }
    })
Higgs
  • 451
  • 4
  • 16
Cemal BAYRI
  • 353
  • 4
  • 10
  • 1
    i ll update my answer and write the code a used, because i couldnt find how to write code as a comment from here.. Sorry for late.@Badr Filali – Cemal BAYRI Apr 10 '17 at 12:23
  • 1
    Worked for me, but my body need be a JSON, so, I changed the encoding: URLEncoding.httpBody to encoding: JSONEncoding.default and everything worked well. – Ângelo Polotto Aug 09 '18 at 13:59
  • Yes @AngeloPolotto thanks for contrubition:) This is about encoding type of service. Can be used depending your rest api as URLEncoding or JSONEncoding. – Cemal BAYRI Aug 09 '18 at 14:15
  • Thank you for the answer. But the problem I am facing is when I send a variable instead of `double quotes strings` and get error code `400`. How would I solve that? – viper Aug 15 '18 at 12:30
  • Can you send me the example code may be i can help you better with this way @viper – Cemal BAYRI Aug 16 '18 at 14:33
  • This is what I did :- let parameters = [ "password":"Admin@135", "grant_type":"password", "client_id":"Sipradi-mbl", "client_secret":"295265f0-c060-4782-9688-36854dba9af5", "scope":"offline_access", "username": edtEmail?.text ] @CemalBAYRI – viper Aug 17 '18 at 07:00
  • @viper i wanted you share me the fetching func that you called Alamofire. I cannot see any problem in this code line. if it is possible i can help you better – Cemal BAYRI Aug 17 '18 at 10:18
  • I am not getting problem while fetching data if I replace `edtEmail?.text` or any dynamic value with a static double quotes string. – viper Aug 17 '18 at 10:45
  • Did you unwrap "edtMail.text?" ?? May be your textfields or etc. items are nil? – Cemal BAYRI Aug 17 '18 at 10:50
8

I modified @Silmaril's answer to extend Alamofire's Manager. This solution uses EVReflection to serialize an object directly:

//Extend Alamofire so it can do POSTs with a JSON body from passed object
extension Alamofire.Manager {
    public class func request(
        method: Alamofire.Method,
        _ URLString: URLStringConvertible,
          bodyObject: EVObject)
        -> Request
    {
        return Manager.sharedInstance.request(
            method,
            URLString,
            parameters: [:],
            encoding: .Custom({ (convertible, params) in
                let mutableRequest = convertible.URLRequest.copy() as! NSMutableURLRequest
                mutableRequest.HTTPBody = bodyObject.toJsonString().dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
                return (mutableRequest, nil)
            })
        )
    }
}

Then you can use it like this:

Alamofire.Manager.request(.POST, endpointUrlString, bodyObject: myObjectToPost)
raf
  • 2,339
  • 17
  • 19
7

Based on Illya Krit's answer

Details

  • Xcode Version 10.2.1 (10E1001)
  • Swift 5
  • Alamofire 4.8.2

Solution

import Alamofire

struct BodyStringEncoding: ParameterEncoding {

    private let body: String

    init(body: String) { self.body = body }

    func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest {
        guard var urlRequest = urlRequest.urlRequest else { throw Errors.emptyURLRequest }
        guard let data = body.data(using: .utf8) else { throw Errors.encodingProblem }
        urlRequest.httpBody = data
        return urlRequest
    }
}

extension BodyStringEncoding {
    enum Errors: Error {
        case emptyURLRequest
        case encodingProblem
    }
}

extension BodyStringEncoding.Errors: LocalizedError {
    var errorDescription: String? {
        switch self {
            case .emptyURLRequest: return "Empty url request"
            case .encodingProblem: return "Encoding problem"
        }
    }
}

Usage

Alamofire.request(url, method: .post, parameters: nil, encoding: BodyStringEncoding(body: text), headers: headers).responseJSON { response in
     print(response)
}
Vasily Bodnarchuk
  • 19,860
  • 8
  • 111
  • 113
5

If you want to post string as raw body in request

return Alamofire.request(.POST, "http://mywebsite.com/post-request" , parameters: [:], encoding: .Custom({
            (convertible, params) in
            let mutableRequest = convertible.URLRequest.copy() as! NSMutableURLRequest

            let data = ("myBodyString" as NSString).dataUsingEncoding(NSUTF8StringEncoding)
            mutableRequest.HTTPBody = data
            return (mutableRequest, nil)
        }))
J.R
  • 1,985
  • 16
  • 20
  • Hi JITHINRAJ I posted a question here http://stackoverflow.com/questions/40385992/how-to-send-a-xml-object-with-alamofire I want to send a xml body – mike vorisis Nov 02 '16 at 21:04
5

I have done it for array from strings. This solution is adjusted for string in body.

The "native" way from Alamofire 4:

struct JSONStringArrayEncoding: ParameterEncoding {
    private let myString: String

    init(string: String) {
        self.myString = string
    }

    func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest {
        var urlRequest = urlRequest.urlRequest

        let data = myString.data(using: .utf8)!

        if urlRequest?.value(forHTTPHeaderField: "Content-Type") == nil {
            urlRequest?.setValue("application/json", forHTTPHeaderField: "Content-Type")
        }

        urlRequest?.httpBody = data

        return urlRequest!
    }
}

And then make your request with:

Alamofire.request("your url string", method: .post, parameters: [:], encoding: JSONStringArrayEncoding.init(string: "My string for body"), headers: [:])
Zonily Jame
  • 3,917
  • 2
  • 22
  • 43
Illya Krit
  • 725
  • 1
  • 6
  • 8
4

I've used answer of @afrodev as reference. In my case I take parameter to my function as string that have to be posted in request. So, here is the code:

func defineOriginalLanguage(ofText: String) {
    let text =  ofText
    let stringURL = basicURL + "identify?version=2018-05-01"
    let url = URL(string: stringURL)

    var request = URLRequest(url: url!)
    request.httpMethod = HTTPMethod.post.rawValue
    request.setValue("text/plain", forHTTPHeaderField: "Content-Type")
    request.httpBody = text.data(using: .utf8)

    Alamofire.request(request)
        .responseJSON { response in
            print(response)
    }
}
GeoSD
  • 384
  • 5
  • 7
2
func paramsFromJSON(json: String) -> [String : AnyObject]?
{
    let objectData: NSData = (json.dataUsingEncoding(NSUTF8StringEncoding))!
    var jsonDict: [ String : AnyObject]!
    do {
        jsonDict = try NSJSONSerialization.JSONObjectWithData(objectData, options: .MutableContainers) as! [ String : AnyObject]
        return jsonDict
    } catch {
        print("JSON serialization failed:  \(error)")
        return nil
    }
}

let json = Mapper().toJSONString(loginJSON, prettyPrint: false)

Alamofire.request(.POST, url + "/login", parameters: paramsFromJSON(json!), encoding: .JSON)
AndrewK
  • 877
  • 8
  • 14
2

My case, posting alamofire with content-type: "Content-Type":"application/x-www-form-urlencoded", I had to change encoding of alampfire post request

from : JSONENCODING.DEFAULT to: URLEncoding.httpBody

here:

let url = ServicesURls.register_token()
    let body = [
        "UserName": "Minus28",
        "grant_type": "password",
        "Password": "1a29fcd1-2adb-4eaa-9abf-b86607f87085",
         "DeviceNumber": "e9c156d2ab5421e5",
          "AppNotificationKey": "test-test-test",
        "RegistrationEmail": email,
        "RegistrationPassword": password,
        "RegistrationType": 2
        ] as [String : Any]


    Alamofire.request(url, method: .post, parameters: body, encoding: URLEncoding.httpBody , headers: setUpHeaders()).log().responseJSON { (response) in
IsPha
  • 355
  • 4
  • 8
0
let parameters = ["foo": "bar"]
              
    // 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))
    
    
Mohamed AbdelraZek
  • 1,557
  • 3
  • 15
  • 27
-3

Xcode 8.X , Swift 3.X

Easy Use;

 let params:NSMutableDictionary? = ["foo": "bar"];
            let ulr =  NSURL(string:"http://mywebsite.com/post-request" as String)
            let request = NSMutableURLRequest(url: ulr! as URL)
            request.httpMethod = "POST"
            request.setValue("application/json", forHTTPHeaderField: "Content-Type")
            let data = try! JSONSerialization.data(withJSONObject: params!, options: JSONSerialization.WritingOptions.prettyPrinted)

            let json = NSString(data: data, encoding: String.Encoding.utf8.rawValue)
            if let json = json {
                print(json)
            }
            request.httpBody = json!.data(using: String.Encoding.utf8.rawValue);


            Alamofire.request(request as! URLRequestConvertible)
                .responseJSON { response in
                    // do whatever you want here
                   print(response.request)  
                   print(response.response) 
                   print(response.data) 
                   print(response.result)

            }
SwiftDeveloper
  • 6,588
  • 11
  • 50
  • 75