Twitter API 1.1 - update_with_media returns 500

I'm trying to upload a picture on Twitter from a Mac app, using REST API 1.1 (url: https://api.twitter.com/1.1/statuses/update_with_media.json), but I get always error code 500 and {"errors":[{"message":"Internal error","code":131}]}. If I upload only a tweet (using /update.json) it works fine every time.

Here is my code:

NSURL *url = [NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/update_with_media.json"];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30.0f];
[request setHTTPMethod:@"POST"];
[request setHTTPShouldHandleCookies:NO];

NSString *boundary = @"64F3EC90-E32B-4BD9-ADB4-E1A9FBE4AFD6";

NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request setValue:contentType forHTTPHeaderField:@"Content-Type"];

[self signRequest:request];  // Adding Oauth

NSMutableData *body = [NSMutableData dataWithLength:0];

// Adding tweet string
[body appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSData *data = [[NSString stringWithFormat:@"%@\r\n",@"Uploading again test3"]dataUsingEncoding:NSUTF8StringEncoding];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n",@"status"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:data];

// Adding image
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"media[]\"; filename=\"test.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: image/jpeg\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Transfer-Encoding: binary\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];

[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

// Set HTTPBody
[request setValue:@(body.length).stringValue forHTTPHeaderField:@"Content-Length"];
request.HTTPBody = body;

Answers


I had luck with the following code:

- (void)postTweet:(NSString *)tweetString withImageData:(NSData *)imageData {

NSURL *baseURL = [NSURL URLWithString:url_statuses_update_with_media];

NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"status"] = tweetString;
params[@"media[]"] = imageData;

[self sendPOSTRequestForURL:baseURL andParams:params];}                                    

-(NSError *)sendPOSTRequestForURL:(NSURL *)url andParams:(NSDictionary *)params { NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30.0f]; [request setHTTPMethod:@"POST"]; [request setHTTPShouldHandleCookies:NO];

NSString *boundary = @"64F3EC90-E32B-4BD9-ADB4-E1A9FBE4AFD6";

NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request setValue:contentType forHTTPHeaderField:@"Content-Type"];

[self signRequest:request];

NSMutableData *body = [NSMutableData dataWithLength:0];

for (NSString *key in params.allKeys) {
    id obj = params[key];

    [body appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    NSData *data = nil;

    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n",key] dataUsingEncoding:NSUTF8StringEncoding]];

    if ([obj isKindOfClass:[NSData class]]) {
        [body appendData:[@"Content-Type: application/octet-stream\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        data = (NSData *)obj;
    } else if ([obj isKindOfClass:[NSString class]]) {

        data = [[NSString stringWithFormat:@"%@",(NSString *)obj]dataUsingEncoding:NSUTF8StringEncoding];
    }

    [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:data];
    [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
}

[body appendData:[[NSString stringWithFormat:@"--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];


[request setValue:@(body.length).stringValue forHTTPHeaderField:@"Content-Length"];
request.HTTPBody = body;


Need Your Help

Binary search tree segmentation fault

c++ class function-pointers binary-search-tree

This program is meant to take strings from an input file, sort them alphabetically to make a signature, then insert the signature as a key for a BST node. The word the signature was created from is...