1

I want to send a POST request with Alamofire SessionManager.

I read the documentation on https://github.com/Alamofire/Alamofire/blob/master/Documentation/Alamofire%204.0%20Migration%20Guide.md#parameter-encoding-protocol

but I don't see an example for using request and POST, only upload.

The examples it gives:

let parameters: Parameters = ["foo": "bar"]

Alamofire.request(urlString, parameters: parameters) // Encoding => URLEncoding(destination: .methodDependent)
Alamofire.request(urlString, parameters: parameters, encoding: URLEncoding(destination: .queryString))
Alamofire.request(urlString, parameters: parameters, encoding: URLEncoding(destination: .httpBody))

// Static convenience properties (we'd like to encourage everyone to use this more concise form)
Alamofire.request(urlString, parameters: parameters, encoding: URLEncoding.default)
Alamofire.request(urlString, parameters: parameters, encoding: URLEncoding.queryString)
Alamofire.request(urlString, parameters: parameters, encoding: URLEncoding.httpBody)

My code is:

manager.request(url, method: .post, parameters: parameters, encoding: .url) .responseJSON { response in fulfill(response) }

which conforms to the method signature (from what I can tell) but I get an error "Extra parameter method: in call.

quantumpotato
  • 9,637
  • 14
  • 70
  • 146
  • check this answer by myself https://stackoverflow.com/questions/44484772/how-to-post-nested-json-by-swiftyjson-and-alamofire/44500753#44500753 maybe can help you, let me know – Reinier Melian Jun 19 '17 at 20:44

1 Answers1

1

You need conform URLRequest with your parameters in body as Data

This code can help you

    var request = URLRequest(url: urlString!)
    request.httpMethod = "POST"
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")

    request.httpBody = try! JSONSerialization.data(withJSONObject: parameters)

    manager.request(request)
           .responseJSON { response in
                fulfill(response)
        }

Hope this helps

Reinier Melian
  • 20,519
  • 3
  • 38
  • 55
  • Hello, this is working for me and I will accept. However I have an issue with returning this Promise: https://stackoverflow.com/questions/44640197/cannot-convert-return-expression-of-type-promise-datarequest-to-return – quantumpotato Jun 19 '17 at 21:24
  • @quantumpotato I don't have any experience with Promise but you can check this pod maybe can help you https://github.com/PromiseKit/Alamofire- – Reinier Melian Jun 19 '17 at 21:36