0

I am trying to do a .POST request with Alamofire in Swift 3. I´ve written the following function

func postToken(Token: String) {
    let parameters : [String:Any] = ["api_key":"ivaomobileapp", "function":"login", "IVAOTOKEN=":"\(Token)"]
    Alamofire.request("URL", method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: nil).responseJSON { (response:DataResponse<Any>) in

        switch(response.result) {
        case .success(_):
            if let data = response.result.value{
                print(data)
            }
            break

        case .failure(_):
            print(response.result.error as Any)
            break

        }
    }
}

But the code doesn´t work, it gives the following error:

Alamofire.AFError.responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}))

This is the same request in CURL(UNIX)

curl https://whatever -X POST -F 'api_key=ivaomobileapp' -F 'function=Login' -F 'IVAOTOKEN=whatever'

What am I doing wrong?

Thanks

CTABUYO
  • 662
  • 2
  • 7
  • 27

2 Answers2

0

Response from a server isn't valid json try using responseString, responseData or response to figure out what the issue is.

Ram
  • 961
  • 6
  • 14
0

I think you handle the addition of the IVAOTOKEN parameter in the wrong way, causing issues, perhaps creating a malformed dictionary. Perhaps your parameters should look like the following:

let parameters : [String:Any] = [
      "api_key": "ivaomobileapp", 
      "function": "login", 
      "IVAOTOKEN": Token
]

Alamofire will add the quotes around the Token variable, as it is a String. The result should be that the following is sent to the server:

{
    "api_key": "ivaomobileapp", 
    "function": "login", 
    "IVAOTOKEN": "TOKENVALUE"
}
Wolfgang Schreurs
  • 11,779
  • 7
  • 51
  • 92
  • I have a problem sending the api_key parameter, in the CURL request it is as follows 'api_key=ivaomobileapp'. Should I put somewhere the = in the Alamofire. Sorry, I´ve never done POST Requests with Alamofire :( – CTABUYO Feb 04 '17 at 16:11
  • Well, do you literally use the value `ivaomobileapp` for the `api_key` parameter? I would think a key would look actually look somewhat different. Maybe like some sort of random hash. Either way, the `=` sign should not be part of the parameter key. Alamofire will transform your parameters to valid JSON and as such will add the `=` sign between the key and the value. – Wolfgang Schreurs Feb 04 '17 at 16:13
  • I think you should make 100% sure you can use this value as API key, as it's highly unusual. Usually it's a random hash or some such. Especially if you compare it with API keys from Facebook, Twitter, Flickr, etc... – Wolfgang Schreurs Feb 04 '17 at 16:15
  • yeah completely sure, CURL works with that value. My Chinese partner made the API so maybe it´s a mistranslation between Mandarin and English. – CTABUYO Feb 04 '17 at 16:16
  • Well, another thing that's different with your curl request, is that in your curl request you actually don't send JSON, but you use a form request. So you might want to change a different kind of request. Or transform your curl request to a JSON request, if your server supports this. – Wolfgang Schreurs Feb 04 '17 at 16:20
  • Perhaps try a form request like here, that should be mostly similar with the curl request you are using: http://stackoverflow.com/a/37178808/250164 – Wolfgang Schreurs Feb 04 '17 at 16:25