views:

1170

answers:

1

Can anybody familiar with the source code for uploading image to Twitter using "Twitpic api" please tell why I get a 0 response code when I am trying to upload an image?

Here is my code:

- (BOOL)uploadImageToTwitpic:(UIImage*)image
                 withMessage:(NSString*)theMessage
                    username:(NSString*)username
                    password:(NSString*)password
{
    NSString *stringBoundary, *contentType, *message, *baseURLString, *urlString;
    NSData *imageData;
    NSURL *url;
    NSMutableURLRequest *urlRequest;
    NSMutableData *postBody;

    // Create POST request from message, imageData, username and password
    baseURLString = kTwitpicUploadURL;
    urlString = [NSString stringWithFormat:@"%@", baseURLString];
    url = [NSURL URLWithString:urlString];
    urlRequest = [[[NSMutableURLRequest alloc] initWithURL:url] autorelease];
    [urlRequest setHTTPMethod:@"POST"];

    // Set the params
    message = ([theMessage length] > 1) ? theMessage : @"Here's my new Light Table collage.";
    imageData = UIImageJPEGRepresentation(image, kTwitpicImageJPEGCompression);

    // Setup POST body
    stringBoundary = [NSString stringWithString:@"0xKhTmLbOuNdArY"];
    contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", stringBoundary];
    [urlRequest addValue:contentType forHTTPHeaderField:@"Content-Type"];

    // Setting up the POST request's multipart/form-data body
    postBody = [NSMutableData data];
    [postBody appendData:[[NSString stringWithFormat:@"\r\n\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"source\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:@"lighttable"] dataUsingEncoding:NSUTF8StringEncoding]]; // So Light Table show up as source in Twitter post

    [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"username\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:username] dataUsingEncoding:NSUTF8StringEncoding]]; // username

    [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"password\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:password] dataUsingEncoding:NSUTF8StringEncoding]]; // password

    [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"message\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:message] dataUsingEncoding:NSUTF8StringEncoding]]; // message

    [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"media\"; filename=\"%@\"\r\n", @"lighttable_twitpic_image.jpg" ] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:@"Content-Type: image/jpg\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; // jpeg as data
    [postBody appendData:[[NSString stringWithString:@"Content-Transfer-Encoding: binary\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:imageData]; // Tack on the imageData to the end

    [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [urlRequest setHTTPBody:postBody];
    NSLog(@"data=======>%@",postBody);
    NSLog(@"URLReq========>%@",urlRequest);
    // Spawn a new thread so the UI isn't blocked while we're uploading the image
    [NSThread detachNewThreadSelector:@selector(uploadingDataWithURLRequest:) toTarget:self withObject:urlRequest];

    return YES; // TODO: Should raise exception on error
}

- (void)uploadingDataWithURLRequest:(NSURLRequest*)urlRequest {
    // Called on a separate thread; upload and handle server response

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    [urlRequest retain]; // Retain since we autoreleased it before

    // Send the request
    NSHTTPURLResponse *urlResponse;
    NSError *error;
    NSData *responseData = [NSURLConnection sendSynchronousRequest:urlRequest
                                                 returningResponse:&urlResponse
                                                             error:&error];
    NSString *responseString = [[NSString alloc] initWithData:responseData
                                                     encoding:NSUTF8StringEncoding];

    // Handle the error or success
    // If error, create error message and throw up UIAlertView
    NSLog(@"Response Code: %d", [urlResponse statusCode]);
    if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300) {
        NSLog(@"urlResultString: %@", responseString);

        NSString *match = [responseString stringByMatching:@"http[a-zA-Z0-9.:/]*"]; // Match the URL for the twitpic.com post
        NSLog(@"match: %@", match);

        // Send back notice to delegate
        [delegate twitpicEngine:self didUploadImageWithResponse:match];
    }
    else {
        NSLog(@"Error while uploading, got 400 error back or no response at all: %@", [urlResponse statusCode]);
        [delegate twitpicEngine:self didUploadImageWithResponse:nil]; // Nil should mean "upload failed" to the delegate
    }

    [pool drain];
    [responseString release];
    [urlRequest release];
}
+1  A: 
Quinn Taylor
https://twitpic.com/api/uploadAndPost at this URL I get response code as 0 and response data as null.So, wnat can I do for that??
jaynaiphone
I still haven't a clue — the Twitpic API should document error cases like yours. What is the status code for the response? How thoroughly have you checked your POST body against what the API expects?
Quinn Taylor