Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MultipartFormData upload not working with larger files - Message too long error #3669

Closed
DanielZanchi opened this issue Dec 5, 2022 · 3 comments
Assignees
Labels
awaiting response Issues awaits response from reporter.

Comments

@DanielZanchi
Copy link

I am currently using Alamofire 5.6.2 and trying to upload one image with POST request.

The image is 10MB size, if I upload an image around 5MB everything works fine.

Creating the POST request from Postman everything works fine, so it's not an issue related with the server.

This is how I'm creating the request:

AF.upload(multipartFormData: { multipartFormData in
multipartFormData.append(InputStream(data: imageMedia.data), // 10MB data
                         withLength: UInt64(imageMedia.data.count),
                         name: imageMedia.type.rawValue,
                         fileName: imageMedia.filename,
                         mimeType: imageMedia.mimeType)
}, with: request).responseData { afDataResponse in
    switch afDataResponse.result {
    case .success(let data):
        completion(data)
    case .failure(let error):
        print("error with alamofire response: \(error.localizedDescription)")
        completion(nil)
    }
} 

And this is the error I am receiving in the failure case:
error with alamofire response: URLSessionTask failed with error: The operation couldn’t be completed. Message too long

If I print the error (not localizedDescription):

sessionTaskFailed(error: Error Domain=NSPOSIXErrorDomain Code=40 "Message too long" UserInfo={_NSURLErrorFailingURLSessionTaskErrorKey=LocalUploadTask <8F7404DD-2947-4B18-9F23-C23F0483F440>.<1>, _kCFStreamErrorDomainKey=1, NSErrorPeerAddressKey=<CFData 0x600001068280 [0x1b9553c60]>{length = 16, capacity = 16, bytes = 0x100201bbd8ef26350000000000000000}, _kCFStreamErrorCodeKey=40, _NSURLErrorRelatedURLSessionTaskErrorKey=(
    "LocalUploadTask <8F7404DD-2947-4B18-9F23-C23F0483F440>.<1>"
)})

Any idea what's happening here?

@jshier
Copy link
Contributor

jshier commented Dec 10, 2022

I saw on StackOverflow that this was only a problem on the simulator, not on device? If so, this seems like an Apple change you'll want to report and investigate.

One thing you can try, though, is to ensure the upload is always streams from disk rather than entirely from memory. Currently the threshold for switching between those two approaches is exactly 10_000_000 bytes, so I don't know whether your upload is hitting it or not. You can add usingThreshold: 0 in your upload call to make sure it's entirely streamed and see if that avoids the issue.

Also, if you have a project which replicates the issue we can track whether the behavior changes over time.

@jshier jshier self-assigned this Dec 10, 2022
@jshier jshier added the awaiting response Issues awaits response from reporter. label Dec 10, 2022
@jshier
Copy link
Contributor

jshier commented Jul 21, 2023

This seems like it was an intermittent issue with particular simulator versions, so there's probably nothing to do here.

@jshier jshier closed this as completed Jul 21, 2023
@MohammadAlBarari
Copy link

Same Error occuered with me when trying to send a large request body on real device

"Alamofire. AFError.sessionTaskFailed(error: Error
Domain=NSPOSIXErrorDomain Code=40 "Message too long"
Userinfo={_NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask.<42>, _KCFStreamErrorDomainKey=1,
NSErrorPeerAddressKey=<CFData0×600000f622e0[Ox1bbb34418]>{length=28,capacity
= 28, bytes = 0x1c1e01bb000000002606470030330000 ... 6815067500000000},
_kCFStreamErrorCodeKey=40,_NSURLErrorRelatedURLSessionTaskErrorKey=(
"LocalDataTask.<42>"
)}))"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting response Issues awaits response from reporter.
Projects
None yet
Development

No branches or pull requests

3 participants