Alamofire invalid value around character 0

前端 未结 11 1949
忘了有多久
忘了有多久 2020-11-29 02:26
Alamofire.request(.GET, \"url\").authenticate(user: \"\", password: \"\").responseJSON() {
    (request, response, json, error) in
    println(error)
    println(jso         


        
相关标签:
11条回答
  • 2020-11-29 03:17

    In my case , my server URL was incorrect. Check your server URL !!

    0 讨论(0)
  • 2020-11-29 03:18

    The same issue happened to me and it actually ended up being a server issue since the content type wasn't set.

    Adding

    .validate(contentType: ["application/json"])
    

    To the request chain solved it for me

    Alamofire.request(.GET, "url")
            .validate(contentType: ["application/json"])
            .authenticate(user: "", password: "")
            .responseJSON() { response in
                switch response.result {
                case .Success:
                    print("It worked!")
                    print(response.result.value)
                case .Failure(let error):
                    print(error)
                }
            }
    
    0 讨论(0)
  • 2020-11-29 03:19

    In my case, there was an extra / in the URL .

    0 讨论(0)
  • 2020-11-29 03:20

    This is how I managed to resolve the Invalid 3840 Err.

    The error log

     responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}))
    
    1. It was with Encoding Type used in the Request, The Encoding Type used should be acceptedin your Server-Side.

    In-order to know the Encoding I had to run through all the Encoding Types:

    default/ methodDependent/ queryString/ httpBody

        let headers: HTTPHeaders = [
            "Authorization": "Info XXX",
            "Accept": "application/json",
            "Content-Type" :"application/json"
        ]
    
        let parameters:Parameters = [
            "items": [
                    "item1" : value,
                    "item2": value,
                    "item3" : value
            ]
        ]
    
        Alamofire.request("URL",method: .post, parameters: parameters,encoding:URLEncoding.queryString, headers: headers).responseJSON { response in
            debugPrint(response)
         }
    
    1. It also depends upon the response we are recieving use the appropriate
      • responseString
      • responseJSON
      • responseData

    If the response is not a JSON & just string in response use responseString

    Example: in-case of login/ create token API :

    "20dsoqs0287349y4ka85u6f24gmr6pah"

    responseString

    0 讨论(0)
  • 2020-11-29 03:21

    Hey guys this is what I found to be my issue: I was calling Alamofire via a function to Authenticate Users: I used the function "Login User" With the parameters that would be called from the "body"(email: String, password: String) That would be passed

    my errr was exactly:

    optional(alamofire.aferror.responseserializationfailed(alamofire.aferror.responseserializationfailurereason.jsonserializationfailed(error domain=nscocoaerrordomain code=3840 "invalid value around character 0." userinfo={nsdebugdescription=invalid value around character 0

    character 0 is the key here: meaning the the call for the "email" was not matching the parameters: See the code below

    func loginUser(email: String, password: String, completed: @escaping downloadComplete) { let lowerCasedEmail = email.lowercased()

        let header = [
            "Content-Type" : "application/json; charset=utf-8"
        ]
        let body: [String: Any] = [
            "email": lowerCasedEmail,
            "password": password
        ]
    
        Alamofire.request(LOGIN_USER, method: .post, parameters: body, encoding: JSONEncoding.default, headers: header).responseJSON { (response) in
            if response.result.error == nil {
    
                if let data = response.result.value as? Dictionary<String, AnyObject> {
                    if let email = data["user"] as? String {
                        self.userEmail = email
                        print(self.userEmail)
                    }
                    if let token = data["token"] as? String {
                        self.token_Key = token
                        print(self.token_Key)
                    }
    

    "email" in function parameters must match the let "email" when parsing then it will work..I no longer got the error...And character 0 was the "email" in the "body" parameter for the Alamofire request:

    Hope this helps

    | improve this answer | |
    0 讨论(0)
提交回复
热议问题