I'm using the OAuthConsumer library by Jon Crosby (google code link)
I have successfully set up my project with a access & request token/secret to make basic authorized http requests with the server (GET). However now I need to create a call that uploads an image to the server...(POST)
As far as i've found out, i need to define the entire httpBody myself (correct me if i'm wrong). I found some sample code somewhere that appends data to the httpBody but I have absolutely no idea what i'm doing. I have done so like this:
// create url request
urlRequest = [[[OAMutableURLRequest alloc] initWithURL:urlToServerGoesHere
consumer:authentication.consumer
token:authentication.token
realm:nil
signatureProvider:nil] autorelease];
// apply http method on request
[urlRequest setHTTPMethod:@"POST"];
// define boundary and content-type of file in header of request
NSString* boundary = @"----IMAGE_UPLOAD";
NSString* contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[urlRequest setValue:contentType forHTTPHeaderField:@"Content-Type"];
// Create entire body
NSMutableData* body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"image\"; filename=\"upload.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: image/jpg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[urlRequest setHTTPBody:body];
When applying the httpBody at the end of the code above, an exception is thrown inside the OAMutableRequest. I'm thinking there's something i'm doing wrong or haven't set. It seems to be related with the parameters array which i'm not using...?
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSArray objectAtIndex:]: index 1 beyond bounds [0 .. 0]' *** Call stack at first throw:(0 CoreFoundation 0x00ddc5a9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x00f30313 objc_exception_throw + 44
2 CoreFoundation 0x00dd21cc -[__NSArrayI objectAtIndex:] + 236
3 TwooWrapper 0x0000d310 -[OAMutableURLRequest parameters] + 610
4 TwooWrapper 0x0000d45d -[OAMutableURLRequest _signatureBaseString] + 39
5 TwooWrapper 0x0000c619 -[OAMutableURLRequest prepare] + 213
6 TwooWrapper 0x0000c17c -[OADataFetcher
Does anyone have some information on how to upload a file through Oauth with this library that i'm using, or any suggestions? would be a big help! thx