Multipart/form-data POST request gets timeout and is never sent using AFNetworking

1.5k views Asked by At

I am posting a JPEG image with some textual data to my server using AFNetworking:

- (NSArray *)postPhoto:(NSData *)jpegData withRoomType:(NSString *)roomType andDescription:(NSString *)description {
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
dispatch_queue_t queue = dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0);
__block NSArray *returnedMetaData;

AFHTTPRequestOperation *operation = [self.requestManager POST:[Constants postPhotoURLString] parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

    NSLog(@"Room: %@", roomType);
    [formData appendPartWithFormData:[roomType dataUsingEncoding:NSUTF8StringEncoding]
                                name:@"room"];

    NSLog(@"Description: %@", description);
    [formData appendPartWithFormData:[description dataUsingEncoding:NSUTF8StringEncoding]
                                name:@"description"];

    [formData appendPartWithFileData:jpegData
                                name:@"image"
                            fileName:@"photo.jpg"
                            mimeType:@"image/jpeg"];

} success:^(AFHTTPRequestOperation *operation, id responseObject) {
    returnedMetaData = (NSArray *)responseObject;
    dispatch_semaphore_signal(semaphore);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"%@", error.description);
    dispatch_semaphore_signal(semaphore);
}];

operation.completionQueue = queue;

[operation setUploadProgressBlock:^(NSUInteger __unused bytesWritten,
                                    long long totalBytesWritten,
                                    long long totalBytesExpectedToWrite) {
    NSLog(@"Wrote %lld/%lld", totalBytesWritten, totalBytesExpectedToWrite);
}];

[operation start];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
return returnedMetaData;
}

Every request results in a timeout after 60 seconds:

Error Domain=NSURLErrorDomain Code=-1001 "Time-out van het verzoek." UserInfo=0x15d28d30

Judging from Wireshark, the POST request never even leaves the simulator, and Xcode indicates bandwidth usage is 0. Some other POST requests in my app work fine. Doing the exact same thing in curl as in the code above also works just fine.

Also I checked the URL and no trailing slashes are missing, whatsoever. Copy-pasting the URL to curl and doing a multipart/form-data request does produce the desired outcome.

Am I missing something here?

Thanks!

1

There are 1 answers

3
Yuyutsu On
NSData *imageData = UIImageJPEGRepresentation(YOURIMAGE, 0.5);
NSDictionary * dicParamsToSend  = @{@"room" : room,
                                    @"description" : description };
AFHTTPRequestOperation *op = [manager POST:@"rest.of.url" parameters:dicParamsToSend constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    //do not put image inside parameters dictionary as I did, but append it!
    [formData appendPartWithFileData:imageData name:@"image" fileName:@"photo.jpg" mimeType:@"image/jpeg"];

} success:^(AFHTTPRequestOperation *operation, id responseObject) {

    NSLog(@"Success: %@ <=> %@", operation.responseString, responseObject);

}failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    NSLog(@"Error: %@ <=> %@", operation.responseString, error);

}];
[op start];

This might helps you :)