SerializationFailure error while posting data with Alamofire

response serialization failed alamofire swift
error domain=nscocoaerrordomain code=3840 swift 4
error 3840 alamofire
responseserializationfailed reason
responseserializationfailed in swift
json could not be serialized because of error: the data couldn t be read
json could not be serialized because of error alamofire swift
response could not be serialized, input data was nil or zero length.

I am trying to save some text data and an image to server using Alamofire but I am getting 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:

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.
        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( .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(     // server data
                    print(response.result)   // result of response serialization

                    if let JSON = response.result.value {
                        success!(JSON as! [String: AnyObject])
            case .failure(let error):

Thanks in advance

The iOS code is correct, there was problem in backend code. The json was not being properly formed. I corrected the json formation in backend and it started working fine.

FAILURE: responseSerializationFailed(Alamofire.AFError , Hi im getting this error Optional(Error Domain=com.alamofire.error Code=-6006 "​JSON could not be serialized. Input data was nil or zero length. So this req is not happening, i need in cart send array of unescaped json. object using NSJSONSerialization directory to figure out why the decoding is failing. From our Contribution Guidelines. Asking Questions. We don't use GitHub as a support forum. For any usage questions that are not specific to the project itself, please ask on Stack Overflow instead.

let headers:HTTPHeaders = ["Authorization": "Bearer " + token2Save]
let moreheaders:Parameters = ["Dropbox-API-Arg": ["path":sourcePath]]

Alamofire.request("", 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)"))

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

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


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

JSON serialisation error · Issue #1414 · Alamofire/Alamofire · GitHub, Alamofire is an HTTP networking library written in Swift. HTTP Headers, Authentication; Large Data - Downloading Data to a File, Uploading Data to a Server If no errors occur and the server data is successfully serialized into a JSON object, the responseJSON { response in debugPrint(response) } case .​failure(let  Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question.Provide details and share your research! But avoid …. Asking for help, clarification, or responding to other answers.

FrontEnd send parameters:

"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]) throw error. So we can create a parameter to json.

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

Alamofire, Alamofire+Result is a Fork of Alamofire an HTTP networking library written in Swift with the combination of server data print(response.result) // result of response serialization if let JSON POST Request With URL-Encoded Parameters Failure(error) } do { let XML = try ONOXMLDocument(data: validData) return . Alamofire / Alamofire. Sign up Why GitHub? Features → Code review; Project management

Check the path in the url may be you are missing some folder.

Alamofire-Result on, However, I keep getting error, saying FAILURE Error: feedback submission failed. Optional("The operation couldn't be completed. JSON could not be serialized. When I want to get data from the server using Alamofire it shows me some error  Alamofire / Alamofire. Sign up Why GitHub? Features → Code review; Project management

Swift: Alamofire Post - json - html, So let's look at a few common types of the Alamofire calls in Swift 2.2 and see how There's a section in there on Breaking API Changes that should cover most of the problems we find. GET, urlString).response { request, response, data, error in POST, todosEndpoint, parameters: newTodo, encoding: . Tech support scams are an industry-wide issue where scammers trick you into paying for unnecessary technical support services. You can help protect yourself from scammers by verifying that the contact is a Microsoft Agent or Microsoft Employee and that the phone number is an official Microsoft global customer service number.

Updating Alamofire Calls to Swift 3.0, Alamofire is a Swift-based HTTP networking library for iOS and macOS. and response serialization, authentication, and many other features. In POST: Sends data to the server, commonly used when filling a form and isSuccess, let value = response.result.value else { print("Error while uploading file:  Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported.

Alamofire Tutorial: Getting Started, In my webclient, which just interfaces AFNetworking more or less. 2014-07-03 22:16:50.921 FantasyFeed[9824:60b] POST FAILURE: TASK: ERROR: Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed:​  Alamofire.upload: Upload files with multipart, stream, file or data methods. Download files or resume a download already in progress. Alamofire.request: Every other HTTP request not associated with file transfers. These Alamofire methods are global within Alamofire so you don’t have to instantiate a class to use them.

  • Thanks for the help "Replace responseJSON with responseString and check your response from webservice.. it will lead to get the error line." this helped me