Alamofire invalid value around character 0

前端 未结 11 1955
忘了有多久
忘了有多久 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: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

提交回复
热议问题