SoFunction
Updated on 2025-04-09

Detailed explanation of the example uploading file using application/json in iOS development

This article explains to you the form of uploading files in iOS in application/json through example code. Please refer to this article for details of the specific content.

In the process of interacting with the sever background, sometimes, they need our iOS developers to upload it in the form of "application/json".

NSString *accessUrl = [NSString stringWithFormat:@"%@/xxx",@":xxxx"];
 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:accessUrl]];
  = @"POST";
 //Set request header [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
 //Set request body NSMutableData *body = [NSMutableData data];
 [body appendData:[jsonStr dataUsingEncoding:NSUTF8StringEncoding]];
 [request setHTTPBody:body];
 NSHTTPURLResponse* urlResponse = nil;
 NSError *error = [[NSError alloc] init];
 NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
 NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
 if (result == nil) {
  NSLog(@"Json parsing failed!");
 }
 else
 {
  NSData *jsonData = [result dataUsingEncoding:NSUTF8StringEncoding];
  NSError *err;
  NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
               options:NSJSONReadingMutableContainers
                error:&err];
  if(err) {
   NSLog(@"json parsing failed: %@",err);
  }
  success(dic);
 }

Summarize

The above is a detailed explanation of the examples of uploading files in iOS development using application/json in iOS development. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!