Search code examples
iosafnetworkingmultipartform-dataalamofirealamofireimage

"too many HTTP redirects" while using Alamofire Upload Multipart Form Data


I'm using Alamofire 3 and encountered this:

[Result]: FAILURE: Error Domain=NSURLErrorDomain Code=-1007 "too many HTTP redirects" UserInfo={NSUnderlyingError=0x15eb422d0 {Error Domain=kCFErrorDomainCFNetwork Code=-1007 "(null)"}, NSErrorFailingURLStringKey=URL omitted, NSErrorFailingURLKey= URL omitted, NSLocalizedDescription= too many HTTP redirects}

while calling this method:

upload(.POST, kTaskSubmitImageUrl, multipartFormData: { multipartFormData in
            multipartFormData.appendBodyPart(data: result, name: "uploaded")
            },
            encodingCompletion:{ encodingResult in

                switch encodingResult
                {
                    case .Success(let upload, _, _):
                        upload.responseJSON{
                            responseData in
                            debugPrint(responseData)
                        }
                    case .Failure(let errorStatus) :
                        debugPrint(errorStatus)
                }
            }
        )

anybody know whats going on about it? Should I look at my server?


Solution

  • it turns out that in iOS 9, there's this App Transport Security (ATS) to enforce best practices in secure connections between an app and its back end. Hence the redirect issues.

    by adding Alamofire.Manager.sharedInstance.delegate.taskWillPerformHTTPRedirection = nil

    everything works like magic XD

    Cheers

    Des