Alamofire: How to pass header in POST request

Created on 29 Sep 2016  路  5Comments  路  Source: Alamofire/Alamofire

Hi, I have problem implementing POST request in Swift 3.0.

How to pass header in this request:

Alamofire.request("\(apiUrl)", method: .post, parameters: parameters) .validate(statusCode: 200..<205) .responseString { response in

When I put header next to "parameters" I'm getting error "Extra argument 'method' in call"

My header is:

let headers: HTTPHeaders = [ "X-APP-TOKEN" : Token().readToken() ]

Most helpful comment

I have the same error with header on the right... Any other advice?
FYI: url: String, parameters: [String: String], header: HTTPHeaders
Alamofire.request(url, method: .post, parameters: parameters, encoding: .JSON, headers: headers).responseJSON { (response) in }

All 5 comments

My bad. It should be after parameters, not before :-)
It was before parameters on earlier version.

I have the same error with header on the right... Any other advice?
FYI: url: String, parameters: [String: String], header: HTTPHeaders
Alamofire.request(url, method: .post, parameters: parameters, encoding: .JSON, headers: headers).responseJSON { (response) in }

Me too! How do I pass the headers on a post?

Hi scottrichards,
You can pass header as below, make sure you are using latest version of Alamofire ->4 and swift 3.0
Its working fine for me. Hope it helps :)
Alamofire.request("your_URL", parameters: your_parameters, headers: ["accessToken": "your app token", "Content-type application":"json", "Accept application" : "json"]).responseJSON() { response in }

I also having the same problem..I sending

let headers : HTTPHeaders = [ "Content-Type":"application/x-www-form-urlencoded",authorization" : apiKey ]
Alamofire.request(AppConfig.MyUrl,method: .post ,parameters : params,encoding: JSONEncoding.default ,headers:headers).responseJSON {response in

But the value of authorization cannot get in my webservice.Please help

Was this page helpful?
0 / 5 - 0 ratings

Related issues

shivang2902 picture shivang2902  路  3Comments

Tulleb picture Tulleb  路  3Comments

sarbogast picture sarbogast  路  3Comments

lvandal picture lvandal  路  3Comments

tib picture tib  路  3Comments