0

I am trying to make a POST request for .ts files using NSURLSession. However, what I want to know is, is there a way to do this without converting the .ts files to an NSData object, and without using it inside an NSDictionary? In other words, can I upload a .ts file directly to the intended URL?

Here is the code I am working with:

NSURL *url = [NSURL URLWithString:@"myURL"];
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:config];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
request.HTTPMethod = @"POST";

//I don't want to use the following line 
NSDictionary *dictionary = @{@"key1": @"value1"};
NSError *error = nil;

//I don't want to use the following line
NSData *data = [NSJSONSerialization dataWithJSONObject:dictionary 
  options:kNilOptions error:&error];

if (!error) {

 NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request
   fromData:data completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

   }];

   [uploadTask resume];
}

How can I accomplish this?

halfer
  • 19,824
  • 17
  • 99
  • 186
syedfa
  • 2,801
  • 1
  • 41
  • 74

1 Answers1

1

If you have access to the .ts file locally, you could use

[session uploadTaskRequest:request fromFile:[NSURL youUrlToTheTSFile]]

See the answers here: Uploads using backgroundSessionConfiguration and NSURLSessionUploadTask cause app to crash

Community
  • 1
  • 1
JamesSugrue
  • 14,891
  • 10
  • 61
  • 93
  • Thanks very much for your prompt reply. One follow up question, what is the difference between using the method, withFile: vs fromFile:? – syedfa Apr 20 '15 at 04:07