I am confused with adding headers in 'AFNetworking 3.0'. In the previous version, there is function known as RequestSerializer. How can I add specific headers to a Mutable Request?
{
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://example.com/" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
NSString *path = [[NSBundle mainBundle] pathForResource:@"uploadedImage_3" ofType:@"jpg"];
[formData appendPartWithFileURL:[NSURL fileURLWithPath:path] name:@"file" fileName:@"filename.jpg" mimeType:@"image/jpeg" error:nil];
}
error:nil];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request addValue:@"8b10056e-59e6-4d2b-aa4f-b08f8afff80a" forHTTPHeaderField:@"session_key"];
[request addValue:@"0" forHTTPHeaderField:@"resume_key"];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSURLSessionUploadTask *uploadTask;
uploadTask = [manager
uploadTaskWithStreamedRequest:request
progress:^(NSProgress * _Nonnull uploadProgress) {
// This is not called back on the main queue.
// You are responsible for dispatching to the main queue for UI updates
dispatch_async(dispatch_get_main_queue(), ^{
//Update the progress view
[_progressView setProgress:uploadProgress.fractionCompleted];
});
}
completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (error) {
NSLog(@"Error: %@", error);
} else {
NSLog(@"%@ %@", response, responseObject);
}
}];
[uploadTask resume];
}
Is this is how I should add the headers to the MutableURLRequest?
NSLocalizedDescription = Request failed: unsupported media type (415)}
I am currently getting an error like above.
Found the issue.
"The server is refusing to service the request because the entity of the request is in a format not supported by the requested resource for the requested method."
In other words, the server doesn't support application/json.
I just removed these two lines and it worked...
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];