0
votes

I am attempting to upload multiple images to a server using a multipart form following this example using NSURLSession and NSURLSessionDataTask block based api.

Additionally for all calls made to the server I am sending an authentication token that verifies I am authorized to do the upload.

Everything is working well except for the case when I do the upload and my authentication token has expired. When the token has expired I should be getting an HTTP 400 response with a body saying that the expired token is the reason for the failure (this works on all other calls to the server).

However for the multipart-form upload when my token is expired, I get the 400 response as expected but the NSData object returned in the completion block of

- (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request
                        completionHandler:(void (^)(NSData *data,
                                                    NSURLResponse *response,
                                                    NSError *error))completionHandler

is empty. It's interesting that the NSData object is NOT nil, it's just an empty NSData object.

It's also interesting that in the response object I see a "Content-Length" header which is the length of the response I EXPECT to see, i.e. the "Content-Length" header is greater than 0, however the length of the NSData object IS 0.

So my question boils down to, is there any reason why an NSURLSession would not expose the data returned from the server? Is there something about setting the "multipart-form" header on the request that is making the response have an empty body?

1

1 Answers

0
votes

I'm assuming that when you say "returned from the completion block", you really mean "when examined from within the completion block".

If there's an error set, then an empty object may indicate that the connection timed out while waiting for the server to actually send data.

Otherwise, I have this vague recollection that NSURLSession tasks don't retrieve the body of the error page if the server returns certain result codes, and that you have to do something to change that behavior, like implementing a custom authentication delegate that tells it to load anyway. But this is a vague, distant memory, and I can't find any details about that online, so I could be remembering wrong.