I wanna make function that update profile image with XAuth on iPhone.
but I'm not good at HTTP POST METHOD.
so... anybody.. could you confirm for me whether this Form is right?
The code is like below(I refered this site. and add sendDataReQuest
function http://github.com/aral/XAuthTwitterEngine)

- (NSString *)_sendDataRequestWithMethod:(NSString *)method
                                    path:(NSString *)path
                         queryParameters:(NSDictionary *)params
                                fileData:(NSData *)fileData
                                    body:(NSString *)body
                             requestType:
(MGTwitterRequestType)requestType
                            responseType:
(MGTwitterResponseType)responseType
{
        NSString *fullPath = path;

        NSString *urlString = [NSString stringWithFormat:@"%@://%@/%@",
                           (_secureConnection) ? @"https" : @"http",
                           _APIDomain, fullPath];

        NSLog(@"%@",fullPath);

    NSURL *finalURL = [NSURL URLWithString:urlString];
    if (!finalURL) {
        return nil;
    }

        if (![self isAuthorized])
        {
                return nil;
        }

        NSLog(@"About to carry out request with access token: %@",
_accessToken);
        OAMutableURLRequest *theRequest = [[[OAMutableURLRequest alloc]
initWithURL:finalURL
                                                                                
                                                           
consumer:self.consumer
                                                                                
                                                                  
token:_accessToken
                                                                                
                                                                  realm: nil
                                                                                
                                          signatureProvider:nil] autorelease];
    if (method) {
        [theRequest setHTTPMethod:method];
    }
    [theRequest setHTTPShouldHandleCookies:NO];

    // Set headers for client information, for tracking purposes at
Twitter.
    [theRequest setValue:_clientName    forHTTPHeaderField:@"X-Twitter-
Client"];
    [theRequest setValue:_clientVersion forHTTPHeaderField:@"X-Twitter-
Client-Version"];
    [theRequest setValue:_clientURL     forHTTPHeaderField:@"X-Twitter-
Client-URL"];

        /*
    NSMutableURLRequest *theRequest = [self
_baseRequestWithMethod:method
 
path:path
 
requestType:requestType
 
queryParameters:params];
        */

    BOOL isPOST = (method && [method isEqualToString:@"POST"]);
    if (isPOST) {
                NSLog(@"This is XEngin Posting Method!");
                NSString *boundary = @"0xKhTmLbOuNdArY";
        NSString *filename = @"image.png";

        NSString *bodyPrefixString   = [NSString stringWithFormat:@"--
%...@\r\n", boundary];
        NSString *bodySuffixString   = [NSString stringWithFormat:@"\r
\n...@--\r\n", boundary];
        NSString *contentDisposition = [NSString
stringWithFormat:@"Content-Disposition: form-data; name=\"image\";
filename=\"%...@\"\r\n", filename];
        NSString *contentImageType   = [NSString
stringWithFormat:@"Content-Type: image/%...@\r\n", [filename
pathExtension]];
        NSString *contentTransfer    = @"Content-Transfer-Encoding:
binary\r\n\r\n";

        NSMutableData *postBody = [NSMutableData data];

        [postBody appendData:[bodyPrefixString
dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO]];
        [postBody appendData:[contentDisposition
dataUsingEncoding:NSUTF8StringEncoding ]];
        [postBody appendData:[contentImageType
dataUsingEncoding:NSUTF8StringEncoding ]];
        [postBody appendData:[contentTransfer
dataUsingEncoding:NSUTF8StringEncoding]];
        [postBody appendData:fileData];
        [postBody appendData:[bodySuffixString
dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO]];
        [theRequest setHTTPBody:postBody];
        NSString *contentType = [NSString stringWithFormat:@"multipart/
form-data; boundary=%@", boundary, nil];
        [theRequest setValue:contentType forHTTPHeaderField:@"Content-
Type"];

    }
    MGTwitterHTTPURLConnection *connection;

    connection = [[MGTwitterHTTPURLConnection alloc]
initWithRequest:theRequest
 
delegate:self
 
requestType:requestType
 
responseType:responseType];
    if (!connection) {
        return nil;
    } else {
        [_connections setObject:connection forKey:[connection
identifier]];
        [connection release];
    }
        NSLog(@"\nsending request...\t%@", [connection identifier]);
    return [connection identifier];
}

Reply via email to