2

I am using the code below to HTTP POST a multi-part web form, including a JPEG image.

Code:

NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
[params setObject:nameField.text forKey:@"name"];
[params setObject:emailField.text forKey:@"email"];
[params setObject:titleField.text forKey:@"title"];
[params setObject:dateString forKey:@"link"];
[params setObject:descriptionTV.text forKey:@"content"];
[params setObject:tag forKey:@"tags"];

NSData* sendData = UIImageJPEGRepresentation(uploadedPhoto.image, 1.0);
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://example.com/"]];
NSMutableURLRequest *afRequest = [httpClient multipartFormRequestWithMethod:@"POST"
                                                                       path:@"form"
                                                                 parameters:params
                                                  constructingBodyWithBlock:^(id <AFMultipartFormData>formData)
                                  {
                                      [formData appendPartWithFileData:sendData
                                                                  name:@"image"
                                                              fileName:@"image.jpeg"
                                                              mimeType:@"image/jpeg"];
                                  }];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:afRequest];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {

    NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
}];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"SUCCESS! %@", operation.responseString);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"FAILED!!! %@", operation.responseString);
}];

[operation start];

Web form:

<input type="text" name="name" id="name" value="" />
<input type="text" name="email" id="email" value="" />
<input type="text" name="title" id="title" value="" />
<input type="text" name="link" id="link" value="" />
<input type="text" name="content" id="content" value="" />
<input type="text" name="tags" id="tags" value="" />
<input type="file" name="image" id="image" value="" />

Every time I run the code, the image upload goes smooth until when the upload progress is about to be done (~99.5% completed). The operation's completionBlockWithSuccess:failure: blocks are never launched, and the app just freezes there.

Can anyone help me out with this? I have no idea what I'm doing wrong here.

Edit:

I did a bit more investigation and found that it does the same even with an invalid request path, but I'm pretty sure I didn't type my form path wrongly. Also, when I replace the constructingBodyWithBlock block with nil, it will work properly.

Edit 2:

I did some snooping on AFNetworking's Github and found a Github issue post and a similar post on Stack Overflow. I tried the using [httpClient enqueueHTTPRequestOperation:operation]; instead of [operation start]; and making the httpClient a singleton, but the same problem still persists.

Community
  • 1
  • 1
Jian Jie
  • 289
  • 1
  • 2
  • 13

1 Answers1

0

Have you tried to print the NSError in the failure callback? Maybe the server is expecting another mime type.

amb
  • 4,798
  • 6
  • 41
  • 68
  • If I remember correctly, it simply returns the webpage's source code (similar to viewing a page source in a web browser). – Jian Jie Aug 16 '13 at 16:42
  • Are you returning a valid JSON in your server-side code? It seems to me that this is not a AFNetworking problem, but a server-side one. – amb Aug 20 '13 at 07:00
  • Unfortunately, I do not have control over the web backend. I also had a hunch that it's a server-sided issue, so my partner is in the midst of changing the backend system to something better documented for the purpose. – Jian Jie Aug 20 '13 at 15:07
  • @Jay, try to mimmic the AFNetworking call with ``cURL`` and check if you're receiving a valid JSON. If not, you have a server-side issue. – amb Aug 20 '13 at 16:46