SerializationFailure error while sending data using Alamofire

I am trying to save some text data and an image to the server using Alamofire, but I get the following error:

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

My code is:

internal func postContent(forApi Name:String, image:UIImage?, withData payload:[String: String], success: ((_ response:[String: AnyObject])->Void)?, failure: ((Error)->Void)?) { //create Alamofire request //if everything was fine call success block with people array passed into it //if failure occurred, call failure block with error. if(isConnectedToNetwork()){ let url = SharedConstants.baseURL+Name print("url "+SharedConstants.baseURL+Name) Alamofire.upload(multipartFormData: { (multipartFormData) in if let img = image { multipartFormData.append(UIImageJPEGRepresentation(img, 0.5)!, withName: "photo_path", fileName: "swift_file.jpeg", mimeType: "image/jpeg") } for (key, value) in payload { multipartFormData.append(value.data(using: .utf8)!, withName: key) } }, to: url, method: .post , headers:nil, encodingCompletion: { (result) in switch result { case .success(let upload, _, _): upload.responseJSON(completionHandler: { (response) in print(response.request) // original URL request print(response.response) // URL response print(response.data) // server data print(response.result) // result of response serialization if let JSON = response.result.value { print(JSON) success!(JSON as! [String: AnyObject]) } else{ failure!(ErrorType.noRecordFound) } }) case .failure(let error): print(error) } }) } else{ failure!(ErrorType.internetNotWorking) } } 

Thanks in advance

0
ios swift alamofire
source share
4 answers

The iOS code is correct, there was a problem in the internal code. Json was not formed correctly. I fixed the json formation in the backend and it started working fine.

+6
source share
 let headers:HTTPHeaders = ["Authorization": "Bearer " + token2Save] let moreheaders:Parameters = ["Dropbox-API-Arg": ["path":sourcePath]] Alamofire.request("https://content.dropboxapi.com/2/files/download", parameters: moreheaders, encoding: URLEncoding(destination: .queryString), headers: headers).responseJSON { feedback in guard feedback.result.value != nil else { print("Error: did not receive data", print("request \(request) feedback \(feedback)")) return 

URLEncoding (destination: .queryString), headers: headers..ResponseJSON

 Replace responseJSON with responseString and check your response from webservice.. it will lead to get the error line. 

Cm:

fooobar.com/questions/847467 / ...

Replace responseJSON with responseString and check your response from the web service .. this will result in an error string.

+2
source share

FrontEnd send options:

  { "delivery_method": [{ "vehicle_type": "walk", "vehicle_id": "1", "vehicle_color": "", "vehicle_brand": "", "vehicle_image": "", "vehicle_number": "", "mode_type": "1", "vehicle_model": "" }, { "vehicle_type": "scooter", "vehicle_id": "4", "vehicle_color": "", "vehicle_brand": "", "vehicle_image": "", "vehicle_number": "", "mode_type": "1", "vehicle_model": "" }] } 

When I parse JSON like this, JSON.parse(params[:delivery_method]) error. So we can create a parameter for JSON.

 data=JSON.parse(params[:delivery_method].to_json) # no exception. 
+1
source share

Check the path in the URL, maybe you are missing a folder.

0
source share

All Articles