7

I am having trouble fixing this error message on my Swift Alamofire POST request (to login a user).

'3840' "Invalid value around character 1."

I have imported Foundation, Alamofire, SwiftyJson. There are no authorisation restrictions (no Oauth etc). I'm also getting the same error message when I change the Post (eg to another endpoint, with other parameters and values) but keep the rest of the code/format the same. On my drupal7 REST server 'definitions' it lists the endpoint as /rest/user/login and parameters as 'username' and 'password' strings as I've used.

I'd really appreciate any tips and help?

error calling REQUEST Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 1." UserInfo={NSDebugDescription=Invalid value around character 1.}

This is my code

 @IBAction func loginButtonTapped(sender: AnyObject) {

    //using Alamofire

    let dataEndpoint: String = "https://www.example.com/rest/user/login"
    let newData = ["username":"Mickey", "password":"123"]
    Alamofire.request(.POST, dataEndpoint, parameters: newData, encoding: .JSON)
        .responseJSON { response in
            guard response.result.error == nil else {
                // got an error in posting the data, need to handle it
                print("error calling REQUEST")
                print(response.result.error!)
                return
            }

            guard let value = response.result.value else {
                print("no result data  when calling request")
                return
            }
            let data = JSON(value)
            print("The result is: " + data.description)
    }

}

Thank you

Dimitri T
  • 921
  • 1
  • 13
  • 27
  • 2
    Possible duplicate of [Alamofire invalid value around character 0](http://stackoverflow.com/questions/32355850/alamofire-invalid-value-around-character-0) – The Tom May 03 '16 at 06:40
  • It just means that the JSON response you get is not valid (or it's not JSON). – Eric Aya May 03 '16 at 08:22

5 Answers5

4

This is an error stating that the response from the server is not valid JSON and contains unreadable characters (e.g. html) Basically it means that your upload did not go well (not accepted by server and therefore not showing json response) or that you did not setup the json response correctly and that it contains html. You can check the latter by looking at the page in your browser and checking the source.

Hope this helps!

Ps: I assume you are using a working URL in your real code right? ;-)

Robski18
  • 306
  • 3
  • 12
  • Thank you, I will check whether the server received the data or not. (Yes, that is a working URL in the real code) – Dimitri T May 03 '16 at 16:16
1

Adding .json at the end of the URL endpoint has solved the error. ie https://www.example.com/rest/user/login.json

Dimitri T
  • 921
  • 1
  • 13
  • 27
1

I got this error when the problem was actually an import error in django on the server side. By following tcp stream in wireshark I got this:

'ImportError': <type 'exceptions.ImportError'>,

DevB2F
  • 4,674
  • 4
  • 36
  • 60
0

I believe error "3840 Invalid value around character 1" is also 404 Page Not Found error

Meaning Alamofire did not actually connect to the server side.

Remember this guidelines:

  1. Double or should I say Triple check your URL endpoints.
  2. Also check if you are using the right parameter encoding.
  3. I suggest you create your Alamofire Router for your API requests. (https://github.com/Alamofire/Alamofire#crud--authorization)
J. Goce
  • 279
  • 3
  • 10
0

If it was working at first and now it fails to serialize then that may happen because your server is down. As server down page (html) would return in response. so don't forget to check your APIError Page in browser.

Emu Malik
  • 101
  • 1
  • 8