Advertisement
Guest User

Untitled

a guest
Feb 25th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  NSData *imgData = UIImagePNGRepresentation(image);
  2.     NSDictionary *aParametersDic; // It's contains other parameters.
  3.     NSURL *url = [NSURL URLWithString:urlString];
  4.     NSMutableURLRequest *request= [[NSMutableURLRequest alloc] initWithURL:url] ;
  5.    
  6.     [request setURL:[NSURL URLWithString:urlString]];
  7.     [request setHTTPMethod:@"POST"];
  8.     NSString *boundary = @"---------------------------14737809831466499882746641449";
  9.     NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
  10.     [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
  11.    
  12.     NSData *jsonData = [NSJSONSerialization dataWithJSONObject:json options:kNilOptions error:nil];
  13.    
  14.     NSMutableData *postbody = [NSMutableData data];
  15.     NSString *postData = [self getHTTPBodyParamsFromDictionary:aParametersDic boundary:boundary];
  16.     [postbody appendData:[postData dataUsingEncoding:NSUTF8StringEncoding]];
  17.    
  18.     [postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  19.     [postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filetype=\"image/png\"; type=\"file\"; filename=\"%@\"\r\n", @"church.jpg"] dataUsingEncoding:NSUTF8StringEncoding]];
  20.     [postbody appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
  21.     [postbody appendData:[NSData dataWithData:imgData]];
  22.     [postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  23.     [postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"data\"; filetype=\"application/json\"; type=\"file\"; filename=\"%@\"\r\n", @"json.josn"] dataUsingEncoding:NSUTF8StringEncoding]];
  24.     [postbody appendData:[@"Content-Type: application/json\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
  25.     [postbody appendData:[NSData dataWithData:jsonData]];
  26.     [postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  27.  
  28.     [request setHTTPBody:postbody];
  29.    
  30.     [[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
  31.         NSString *returnString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  32.        
  33.         NSLog(@"%@\nerror: %@", response, error);
  34.     }] resume];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement