0

I am uploading image to backend.

CURL Command:

curl -i -X POST -F filedata=@file_logo.jpg http://upload.com/projects/
media/add_image/44/8

I am using the below code:

  NSData *editedImage = [NSData dataWithData:UIImagePNGRepresentation(app.ret)];
  NSString *urlString =@"http://upload.com/projects/  
  media/add_image/44/8";
  NSURL *url = [NSURL URLWithString:urlString];
  NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url
                                                            cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                        timeoutInterval:60];

[request setHTTPMethod:@"POST"];
NSString *curlData = @"filedata=";

NSString *boundary =  @"----WebKitFormBoundarycC4YiaUFwM44F6rT";
NSMutableData *body = [NSMutableData data];

NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request setValue:contentType forHTTPHeaderField:@"Content-Type"];

[body appendData:[[NSString stringWithFormat:@"%@",curlData]dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n",@"files1", @"1.png"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:editedImage]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

[request setValue:[NSString stringWithFormat:@"%lu",(unsigned long)[body length]]  forHTTPHeaderField:@"Content-Length"];

[request setHTTPMethod:@"POST"];

[request setHTTPBody:body];

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request
                                                      delegate:self
                                              startImmediately:NO];
[connection start];

Am not getting any response , what wrong am doing here ? Please help. Thanks in advance.

Mac_Play
  • 302
  • 4
  • 21

1 Answers1

0

This might be a "non-answer" but if you're not settled on doing it yourself I can highly recommend AFNetworking for any networking tasks.

Simon
  • 601
  • 1
  • 7
  • 12