Update status with media in iOS with OAuth1


#1

I am struggling to update the authenticating user’s status with media. I have copied the code from this post: https://dev.twitter.com/discussions/15177

POST requests work fine without multipart data and the Authorization header I am submitting(not shown in the below code) should be working fine. The problem lies in the following code I think:

//image 
    NSData *imageData = UIImagePNGRepresentation(img);
    
    NSString *boundary = @"0246824681357ACXZabcxyz";
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
    [request setValue:contentType forHTTPHeaderField:@"content-type"];
    
    NSMutableData *body = [NSMutableData data];
    [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    // status
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"status\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"%@",allParameters[@"status"]] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    // media
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"media[]\"; filename=\"1.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    //[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData  dataWithData:imageData]];
    [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    //
    [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    
    
    NSLog(@"body = %@", [[NSString alloc] initWithData:body encoding:NSASCIIStringEncoding]);
    //END image
 [request setHTTPBody:body];

The body (I use NSASCIIStringEncoding because NSUTF8StringEncoding returns nil due to “wrong encoding”) prints as follows:
Due to “invalid characters” I had to put this on pastebin:
http://pastebin.com/EtcAScuK

The response is, as you can see a 502 error, I am not sure what that means. The tweet will not be submitted successfully, so this request has no effect… Can somebody help me with that ?

2013-07-04 16:06:21.839 MyApp[488:60b] <NSHTTPURLResponse: 0x16dacc70> { URL: https://api.twitter.com/1.1/statuses/update.json?status=mystatus } { status code: 502, headers {
    "Accept-Ranges" = bytes;
    "Content-Encoding" = gzip;
    "Content-Length" = 1353;
    "Content-Type" = "text/html; charset=UTF-8";
    Date = "Thu, 04 Jul 2013 14:05:24 GMT";
    "Last-Modified" = "Fri, 28 Jun 2013 19:39:00 GMT";
    Server = tfe;
    "Strict-Transport-Security" = "max-age=631138519";
    Vary = "Accept-Encoding";
    "x-pad" = "avoid browser bug";
    "x-xss-protection" = "1; mode=block";
} }