54

Getting error while calling Alamofire request method in the latest version(4.0.0).

The syntax is:

Alamofire.request(urlString,method: .post, parameters: requestParams, encoding: .JSON, headers: [:])

the type of requestParam is [String:Any]

CMA
  • 1,528
  • 1
  • 11
  • 22
  • Did you try: `Alamofire.request(urlString, method: .post, parameters: requestParams, encoding: .JSON, headers: [:])` – netdigger Sep 19 '16 at 11:37

15 Answers15

70

I got the issue, I have to use JSONEncoding.default instead of .JSON, so the new syntax is

Alamofire.request(urlString,method: .post, parameters: requestParams, encoding: JSONEncoding.default, headers: [:])
CMA
  • 1,528
  • 1
  • 11
  • 22
50

I can only refer you to: https://github.com/Alamofire/Alamofire/issues/1508#issuecomment-246207682

Basically, if one of your parameters is of the wrong type, the swift compiler will assume you're using request(urlRequest:URLRequestConvertible) and then, the method is an extra argument

Go over that parameters again and make sure all is of correct type (Parameters?, ParameterEncoding, and HTTPHeaders)

netdigger
  • 3,659
  • 3
  • 26
  • 49
  • 2
    Thanks, I got the issue the value of ParameterEncoding was wrong, I have to use JSONEncoding.default instead of .JSON . – CMA Sep 19 '16 at 17:19
  • 2
    This was what caused it to me , I used parameters as string instead of [String:String] – yehyatt May 21 '17 at 13:00
  • Your explanation is what people need. not a dumb response, "do this ...", Thanks! – TheFuquan Jul 23 '17 at 12:51
  • My first parameter was broken. I needed to change the `let urlString = "host"` to `static let urlString = "host"` because the `urlString` variable was attached to the class and I was writing a class function – simple_code Oct 17 '17 at 05:46
  • Thumbs up. One of the best answers on SO! – EmptyStack Feb 15 '18 at 09:13
  • This was my problem. The headers dictionary in my case, was actually [ String : Any ] when I thought it was [ String : String ] (HttpHeaders is just an alias for that). As soon as I forced the bad value to String it compiled. I was storing a token in userDefaults, and when I retrieved it I used object(forKey:) not string(forKey:). – Tom Schulz Sep 21 '18 at 11:16
18

I was having the same issue, the problem is at parameters type, it should be of type [String: Any]. After I made this change, it worked for me.

 Alamofire.request(youUrl, method: .post, parameters: param as? [String: Any], encoding: JSONEncoding.default, headers: [:])
                .responseJSON { response in
Xhulio Hasa
  • 193
  • 1
  • 10
10

It means that some of the parameters type are wrong, check that you are sending these values:

url: String
method: HTTPMethod  (E.g: .post)
parameters: [String:Any]
encoding: ParameterEncoding  (E.g: JSONEncoding.default)
headers: [String: String]
Andrea.Ferrando
  • 987
  • 13
  • 23
10

Updated for Swift 3:

let requestString = "https://thawing-inlet-46474.herokuapp.com/charge.php"
        let params = ["stripeToken": token.tokenId, "amount": "200", "currency": "usd", "description": "testRun"]

        Alamofire.request(requestString,method: .post, parameters: params, encoding: JSONEncoding.default, headers: [:]).responseJSON { (response:DataResponse<Any>) in

            switch(response.result) {
            case .success(_):
                if response.result.value != nil{
                    print("response : \(response.result.value)")
                }
                break

            case .failure(_):
                print("Failure : \(response.result.error)")
                break

            }
        }
Alvin George
  • 14,148
  • 92
  • 64
7

Make sure your parameters is [String: Any]

i.e

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

Not:

let parameters : Parameter = ["foo": "bar"]
Ben
  • 946
  • 1
  • 11
  • 19
5

You are getting that error because of the wrong data types.

Parameters Type should be [String : Any] and parameters type shouldn't be an optional.

Header Type should be [String : String] or nil and header type shouldn't be an optional as well.

Hope it helps. :-)

Mayur Rathod
  • 361
  • 3
  • 13
4

I fixed this by ensuring my requestUrls are strings, and not URL types. Also I removed parameter arguments for when parameter was nil.

E.g.

Alamofire.request(requestUrl, method: .get, encoding: URLEncoding.default)

Phil Hudson
  • 3,819
  • 8
  • 35
  • 59
3

Almofire methods changed in Swift 3 as the following:

  1. Reorder parameters (url then method type).
  2. Change Encoding Enum to be "JSONEncoding.default" for example.
Ahmed Lotfy
  • 3,806
  • 26
  • 28
  • This was the recent issue for me. No hint that it was this, but it rather complained about the HTTPMethod. `.utf8` and similars won't do it, not even just `.default`. It has to be the long name: `JSONEncoding.default` – Alejandro Iván Apr 16 '18 at 18:31
3

This is a family of functions that are very similar, which makes the compiler think you're trying to use a different function. Double check that ALL of the arguments you're supplying are the CORRECT TYPE that is expected. For example I was passing [String:Any] for the variant with headers, which expected [String:String] and got the same error.

Matjan
  • 3,591
  • 1
  • 33
  • 31
  • That's correct. I'm my case i was passing a [String] parameter instead of [String:String] and got the same error too. – mourodrigo Mar 09 '17 at 17:15
3

It is always because im using optional variables in any of the parameters

SoliQuiD
  • 2,093
  • 1
  • 25
  • 29
2

I was facing same problem And try with all answer as previously post here, And then I got the solution and reason of this problem .

This is happened due to pass the wrong object parse in the request, and finally the solution --

theJSONText -- JSON string

urlStr -- URL string

 let urlEncoadedJson = theJSONText.addingPercentEncoding(withAllowedCharacters:.urlHostAllowed)
    let urls = NSURL(string:"urlStr\(urlEncoadedJson ?? "")")

let method: HTTPMethod = .get

Alamofire.request(urls! as URL, method: method, parameters: nil, encoding: JSONEncoding.default, headers: nil).responseJSON { (response:DataResponse<Any>) in

        switch(response.result) {
        case .success(_):

            if response.result.value != nil
            { 
              // write your code here
            }
            break

        case .failure(_):
            if response.result.error != nil
            {
                print(response.result.error!) 
            }
            break
        }

    }

Note - There is no param in my URL .

Abhishek Mishra
  • 1,625
  • 16
  • 32
1

My problem was in optional parameters in the headers that were not unwrapped.

Jakub Vodak
  • 215
  • 3
  • 7
0
Alamofire.request(url, method: .post, parameters: parameters, encoding: 
JSONEncoding.default, headers: [:]).responseJSON 
{ response in

            switch (response.result) {
            case .success:
                print(response)
                break
            case .failure:
                print(Error.self)
            }
        }
Srinivasan_iOS
  • 972
  • 10
  • 12
0

just make sure all the parameters are in the correct format , most importantly the parameters must be in [String:Any] type array.

Suraj Gaur
  • 21
  • 2