How to send NSData using POST from an iOS app?

NSData *imageData = UIImagePNGRepresentation(image);

How to send imageData using POST?

+5
source share
3 answers

The following code should help

NSData *imageData = UIImagePNGRepresentation(image);

NSURL *yourURL = ...
NSMutableURLRequest *yourRequest = [NSMutableURLRequest requestWithURL:yourURL 
                                                           cachePolicy:NSURLRequestUseProtocolCachePolicy 
                                                       timeoutInterval:60.0];
//Set request to post
[yourRequest setHTTPMethod:@"POST"];

//Set content type 
[yourRequest setValue:@"image/png" forHTTPHeaderField:@"Content-Type"];

// Set authorization header if required
...

// set data
[yourRequest setHTTPBody:imageData];


// create connection and set delegate if needed
NSURLConnection *yourConnection = [[NSURLConnection alloc] initWithRequest:yourRequest 
                                                                  delegate:self
                                                          startImmediately:YES];

Please note that you are using ARC.

+10
source

You can check this answer if you agree to using NSURLConnection fooobar.com/questions/1114396 / ...

This post explains how to use the POST audio file, but you can upload any file using the same method.

+2
source

ASIHTTPRequest: http://allseeing-i.com/ASIHTTPRequest/

:

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];

// Upload an NSData instance
[request setData:imageData withFileName:@"myphoto.jpg" andContentType:@"image/jpeg" forKey:@"photo"];

, : http://allseeing-i.com/ASIHTTPRequest/How-to-use

+1

All Articles