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?