Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 21, 2009, 08:44 PM   #1
begdev
macrumors newbie
 
Join Date: Mar 2009
twitpic API help (multipart/form-data upload)

I've put together this code from lots of research.
Information on the twitpic API is here: Twitpic - Share photos on Twitter

Through debugging i can tell that the dictionary is good, and that it gets through all the methods, the request gets submitted, but then the NSLog method returns <>.

I'm not sure where I could be going wrong, i don't know much about the multipart/form-data structure. Maybe I'm doing something wrong with my connection?

Code below.

Code:
-(void)uploadBoth {		
		//Create dictionary of post arguments
		NSArray *keys = [[NSArray alloc] initWithObjects:@"media",@"username",@"password",@"message",nil];
		NSArray *objects = [[NSArray alloc] initWithObjects:
							imageData, twitterUserSave, twitterPassSave, [NSString stringWithFormat:@"%@",messageView.text], nil];
	
		NSDictionary *keysDict = [[NSDictionary alloc] initWithObjects:objects forKeys:keys];
	NSLog(@"%@", keysDict);
		
		//create twitpic photo post
		NSURLRequest *twitpicPost = [self twitpicRequest:keysDict withData:imageData];
		
		//send request, return YES if successful
		NSURLConnection *twitpicConnection = [[NSURLConnection alloc] initWithRequest: twitpicPost delegate:self];
	
		if (!twitpicConnection) {
			NSLog(@"Failed to submit request");
		} else {
			NSLog(@"Request submitted");
			NSData *receivedData = [[NSMutableData data] retain];
			NSLog(@"%@", receivedData); // THIS PART RETURNS <>
		}
}
The NSURLRequest part
Code:
	
-(NSURLRequest *)twitpicRequest:(NSDictionary *)postKeys withData:(NSData *)data {
		
	NSLog(@"got this far");
	
		//create the URL POST Request to twitpic
		NSURL *twitpicURL = [NSURL URLWithString:@"http://twitpic.com/api/uploadAndPost"];
		NSMutableURLRequest *twitpicPost = [NSMutableURLRequest requestWithURL:twitpicURL];
		[twitpicPost setHTTPMethod:@"POST"];
		
		//Add the header info
		NSString *stringBoundary = [NSString stringWithString:@"0xAbCdEfGbOuNdArY"];
		NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",stringBoundary];
		[twitpicPost addValue:contentType forHTTPHeaderField: @"Content-Type"];
		
		//create the body
		NSMutableData *postBody = [NSMutableData data];
		[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
		
		//add key values from the NSDictionary object
		NSEnumerator *keys = [postKeys keyEnumerator];
		int i;
		for (i = 0; i < [postKeys count]; i++) {
			NSString *tempKey = [keys nextObject];
			[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",tempKey] dataUsingEncoding:NSUTF8StringEncoding]];
			[postBody appendData:[[NSString stringWithFormat:@"%@",[postKeys objectForKey:tempKey]] dataUsingEncoding:NSUTF8StringEncoding]];
			[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
		}
		
		//add data field and file data
		[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"data\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
		[postBody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
		[postBody appendData:[NSData dataWithData:data]];
		[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
		
		//add the body to the post
		[twitpicPost setHTTPBody:postBody];
	
	return twitpicPost;
}
begdev is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
getting status messages from facebook using Graph API kingthong iPhone/iPad Programming 2 Jan 21, 2011 04:02 AM
Backup help! robkot iPhone Tips, Help and Troubleshooting 2 Dec 1, 2010 12:04 AM
help with excel for mac 2008 damiang72 Mac Applications and Mac App Store 0 Nov 14, 2010 06:19 AM
video editing help WoblierRex Digital Video 0 Nov 8, 2010 09:23 PM
API help on connecting over wireless to iPhone? whitehexagon iPhone/iPad Programming 3 Nov 28, 2008 02:34 AM


All times are GMT -5. The time now is 10:14 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

Privacy / DMCA contact / Affiliate and FTC Disclosure
Copyright 2002-2013, MacRumors.com, LLC