Uploading image via HTTP POST
Uploading image via HTTP POST
- Subject: Uploading image via HTTP POST
- From: Daniel Meachum <email@hidden>
- Date: Fri, 22 Jan 2010 01:39:20 +0900
My project goal is to upload photos to Tumblr using the Tumblr API found here:
http://www.tumblr.com/docs/api#api_write
I can upload general HTML content no problem but when I attempt to upload a photo I get stumped. The API docs say:
Normal POST method, in which the file's entire binary contents are URL-encoded like any other POST variable. Maximum size:
• 5 MB for videos
• 5 MB for photos
• 5 MB for audio
So I'm trying to get the binary contents of the image according to the API. In my view controller class, I have the following code:
NSData *imageData = [image TIFFRepresentation];
NSBitmapImageRep * bitmap;
bitmap = [NSBitmapImageRep imageRepWithData:imageData];
NSData *pngData = [bitmap representationUsingType:NSPNGFileType properties:nil];
The pngData is sent to the Tumblr post class where I try to upload it.
postType = @"photo";
NSData *imageData = [dataToPost objectForKey:@"image"]; //Comes from view controller pngData variable
request_body = [NSString stringWithFormat:@"email=%@&password=%@&type=%@&data=%@",
[email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
[password stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
[postType stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
[[NSString alloc] initWithData:imageData encoding:NSUTF8StringEncoding]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.tumblr.com/api/write"]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[request_body dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection * theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
The problem is that I can't figure out how to upload the data. I have tried a number of ways but I usually get a Server Response Code 403 - Bad Request. To the best of my knowledge, image data won't convert to NSUTF8StringEncoding-- but how can I fit the data in to the HTTPBody for Tumblr servers to accept? Thanks for your help!_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden