0

I am using Unirest in my objective-c project. It is a very small but handy library.

https://github.com/Mashape/unirest-obj-c

However, I can't wrap my mind around how to POST array of files with Unirest.

If I were to build an HTML form, it will look like:

<input type="file" name="files[]" >
<input type="file" name="files[]" >
<input type="file" name="files[]" >

The file upload example from Unirest is:

NSDictionary* headers = @{@"accept": @"application/json"};
NSURL* file = nil;
NSDictionary* parameters = @{@"parameter": @"value", @"file": file};

UNIHTTPJsonResponse *response = [[UNIRest post:^(UNISimpleRequest *request) {
  [request setUrl:@"http://httpbin.org/post"];
  [request setHeaders:headers];
  [request setParameters:parameters];
}] asJson];

Question: How do I adapt the example for multiple files?

  • The receiving end of this POST request is a PHP page. I can change code there.
Reed
  • 1,628
  • 3
  • 21
  • 29
  • If you observer the example, parameters is a NSDictionary. it one of the key is file, and he added the file as the value. You can add one more value like @"file2":file2Obj . But this all depends on how your server is expecting the files to come in the request. – Priyatham51 Apr 05 '15 at 09:52
  • @Priyatham51 Thanks for helping. I know we can do {@"key":Value}. But in my case the key is `@"files[]"`, then I can't have duplicate keys in NSDictionary. The alternative is to generate @"file[0]" , @"file[1]", @"file[2]"... But I assume UNIREST as a REST library would have this scenario covered. – Reed Apr 05 '15 at 18:20
  • try putting all the files in a array and that add that array to dictionary with key files. – Priyatham51 Apr 05 '15 at 18:29
  • @Priyatham51 just tried; doesn't work. It crashes Unirest library. Guess I'll have to manually put together post parameters. – Reed Apr 05 '15 at 23:29

0 Answers0