0
Alamofire.request(URL_USER_REGISTER, method: .post, parameters: parameters).responseJSON{

                response in
                //printing response
                print(response)

                //getting the json value from the server
                if let result = response.result.value {

                    //converting it as NSDictionary
                    let jsonData = result as! NSDictionary

                    //displaying the message in label
                    self.labelMessage.text = jsonData.value(forKey: "message") as! String?
                }
        }

    }

I am a beginner in Xcode and I got this from a yt tutorial I have this code, but It always gives me responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}))

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
faridorid
  • 33
  • 7
  • It looks like your response isn't JSON. It should start with `{` or `[`. Can you check if your response is correct (is JSON)? You can use some JSON validators available online. Maybe paste response into question... – franiis Nov 11 '17 at 12:48
  • What do you mean? – faridorid Nov 11 '17 at 12:50
  • 2
    Possible duplicate of [Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0](https://stackoverflow.com/questions/42085145/error-domain-nscocoaerrordomain-code-3840-invalid-value-around-character-0) – franiis Nov 11 '17 at 12:50
  • @faridroid You decode response as JSON. But JSON has specified format. If your response isn't formatted in this way, Alamofire won't know how to decode it. You can find more info about JSON format online. – franiis Nov 11 '17 at 12:52
  • What do I have to search for? @franciis – faridorid Nov 11 '17 at 12:54
  • You can do that (use responseString). It will allow you to examine what server is returning. Then you can make sure that it's really JSON. You can use something like `print(response)` and paste the result to question. It's hard to tell what is wrong aside that format could be wrong. If it's really JSON, then you probably want to use responseJSON or parse it form received string. – franiis Nov 11 '17 at 12:57
  • Sorry but I don't really understand what you mean :), where can I learn more about that? – faridorid Nov 11 '17 at 13:04
  • How JSON works: http://json.org/ Alamofire and response handling: https://github.com/Alamofire/Alamofire#response-handling Maybe you should also check out some basic information about how server-client communication works over HTTP. And generally - use print(), debugPrint() or even better debugger to find more information about your objects (eg. print(response) to see what it contains). – franiis Nov 11 '17 at 13:12
  • I still have a problem, but when I used responseString everything, there was written Success, what does it mean? and what is the problem now?Because it writes, that NSDictionary always fails @franiis – faridorid Nov 11 '17 at 13:56
  • Maybe you should `print(response.result.value)` (I'm not sure, but it should give you full response text - I'm not using Mac at the moment to test it). "Success" probably means that you can connect to the server and obtain a response, but like I earlier said - it cannot be read as JSON (and you tried using `responseJSON`). – franiis Nov 11 '17 at 13:58
  • It gave me that:Optional("<\n{\"error\":true,\"message\":\"Some error occurred\"}"), when I used response String and nil when I used responseJSON @franiis – faridorid Nov 11 '17 at 14:06
  • So you know that server doesn't accept your request. There is "some error". Also `<` char in JSON looks suspicious. I suggest that you try to fix your request first. – franiis Nov 11 '17 at 14:15
  • And how to fix it? @franiis – faridorid Nov 11 '17 at 14:18
  • That is a completely different question. I don't know what server you are using, how it works and what error you can generate. I wrote an answer - I'll add some details to it. If you think it helps - accept it :) – franiis Nov 11 '17 at 14:58

1 Answers1

0

After discussion in comments we know that server doesn't accept your message.

Error from question title is caused by invalid JSON returned by the server. Alamofire cannot parse invalid JSON using responseJSON.

You should use responseString method to get a response from the server and you can print it using print(response.result.value). If the server will start to return proper JSON you should try to use responseJSON again or use JSON decoding on response string.

To fix your request you should:

  1. understand hao it should work. Look for some documentation, API docs, examples or tutorials.
  2. compare your solution and try to spot differences,
  3. try to use example solutions or your requests in an application like Postman (or just browser) and see what is happening (what codes server is returning and what is returned in the answer)
  4. when you get working solution, add it your app and check again.

Remember to use proper authentication and HTTP method. Also, check if you're using http/https and what server is expecting.

franiis
  • 1,378
  • 1
  • 18
  • 33