1
votes

I am using AFNetworking 2.1.0 to communicate between my iPad app and my server.

I subclass AFHTTPSessionManager and use the following without problem :

[self POST:kAPIPath
parameters:params
   success:^(NSURLSessionDataTask *task, id responseObject) {
                  successBlock(responseObject[@"result"]);
              }
   failure:^(NSURLSessionDataTask *task, NSError *error) {
                  failureBlock(error);
              }];

But when I add the multipart portion, my server returns

request failure. error: Request failed: service indisponible (503)

So this does not work :

[self POST:kAPIPath
parameters:params
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
                [formData appendPartWithFileData:uploadFile
                                            name:@"file"
                                        fileName:@"photo.jpg"
                                        mimeType:@"image/jpeg"];
                }
   success:^(NSURLSessionDataTask *task, id responseObject) {
                  successBlock(responseObject[@"result"]);
              }
   failure:^(NSURLSessionDataTask *task, NSError *error) {
                  failureBlock(error);
              }];

But it also returns error 503 when I just do this (nothing modifying the formData) :

[self POST:kAPIPath
parameters:params
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

                }
   success:^(NSURLSessionDataTask *task, id responseObject) {
                  successBlock(responseObject[@"result"]);
              }
   failure:^(NSURLSessionDataTask *task, NSError *error) {
                  failureBlock(error);
              }];

or this (nil for the block):

[self POST:kAPIPath
parameters:params
constructingBodyWithBlock:nil
   success:^(NSURLSessionDataTask *task, id responseObject) {
                  successBlock(responseObject[@"result"]);
              }
   failure:^(NSURLSessionDataTask *task, NSError *error) {
                  failureBlock(error);
              }];

I read many posts, but can not understand where this can come from. The error 503 means

503 Service Unavailable The server is currently unavailable (because it is overloaded or down for maintenance).[2] Generally, this is a temporary state. Sometimes, this can be permanent as well on test servers.

But if I use the POST without the multipart, I have no problem. Does anyone have an idea where this could come from ?

Thank you very much !!!

2
5 hundred family errors shows rather a problem on the server side - Julian

2 Answers

1
votes

You were correct, the way of using the multiform is the one below. I'd suggest you to check your server. Try using Postman to debug your multiform directly in your browser.

 [self POST:kAPIPath
parameters:params
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
                [formData appendPartWithFileData:uploadFile
                                            name:@"file"
                                        fileName:@"photo.jpg"
                                        mimeType:@"image/jpeg"];
                }
   success:^(NSURLSessionDataTask *task, id responseObject) {
                  successBlock(responseObject[@"result"]);
              }
   failure:^(NSURLSessionDataTask *task, NSError *error) {
                  failureBlock(error);
              }];
1
votes

I solved my problem.

I think it is the same problem as the one stated and commented there. It seems to be a (recently) known issue in AFNetworking 2.0 with multipart form : issue 1398. I used the workaround proposed by Matt at the bottom, and it's working.

Thanks for your answers.