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")
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:Note:
parameters
should not benil
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.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 allthough you defined it json in your code.
It worked for me..
UPDATED for
Xcode 8.X , Swift 3.X
Easy Use;
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:
You can do this:
Put in httpBody the data