This is the curl command i use to upload PDF's and it works perfectly:
curl -u USERNAME:PASSWORD -T MyPDF.pdf —url https://mywebserver.com
I am using NSURLSession and upload task with delegates but my PDF is never uploaded.
Here is my code:
- (IBAction)upload:(id)sender {
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"https://mywebserver.com/"]];
[request setHTTPMethod:@"POST"];
NSString* pdfPath = [[NSBundle mainBundle] pathForResource:@"bill" ofType:@"pdf"];
NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request fromFile:[NSURL fileURLWithPath:pdfPath]];
[uploadTask resume];
}
- (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session {
}
- (void)URLSession:(NSURLSession *)session task:(nonnull NSURLSessionTask *)task didCompleteWithError:(nullable NSError *)error {
if (error) {
NSLog(@"Error = %@", error);
}
}
- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(nonnull NSURLAuthenticationChallenge *)challenge completionHandler:(nonnull void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler {
completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, [NSURLCredential credentialWithUser:@"username" password:@"password" persistence:NSURLCredentialPersistenceForSession]);
}
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
didReceiveResponse:(NSURLResponse *)response
completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler {
completionHandler(NSURLSessionResponseAllow);
}
- (void)URLSession:(NSURLSession *)session dataTask:(nonnull NSURLSessionDataTask *)dataTask didReceiveData:(nonnull NSData *)data {
if (data) {
NSLog(@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
}
}
- (void)URLSession:(NSURLSession *)session didBecomeInvalidWithError:(nullable NSError *)error {
if (error) {
NSLog(@"Error = %@", error);
}
}