0

I have the following post script written in Objective-C. The code is modified from questions here on SO (the questions are here and here). The photo uploads to the server, however the parameters are not added correctly. Heres the code:

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:30];
[request setHTTPMethod:@"POST"];

NSString *boundary = @"---------------------------14737809831466499882746641449";

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

// post body
NSMutableData *body = [NSMutableData data];

// Dictionary that holds post parameters. 
NSMutableDictionary* _params = [[NSMutableDictionary alloc] init];
[_params setObject:Name forKey:@"username"];

// the boundary string
NSString *BoundaryConstant = @"----------V2ymHFg03ehbqgZCaKO6jy";

// string constant for the post parameter
NSString* FileParamConstant = @"FILE1";

// the server url to which the image 
NSURL* requestURL = [NSURL URLWithString:uploadURLString];

// add params (all params are strings)
for (NSString *param in _params) {
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; charset:UTF-8; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"%@\r\n", [_params objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
}

// add image data
UIImage *newImage = self.urlImageOfUser.image;
NSData *imageData = UIImageJPEGRepresentation(newImage, 1.0);
if (imageData) {
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\n", FileParamConstant] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Type: image/png\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:imageData];
    [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}

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

// setting the body of the post to the request
[request setHTTPBody:body];

// set the content-length
NSString *postLength = [NSString stringWithFormat:@"%d", [body length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];

// set URL
[request setURL:requestURL];

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

NSLog(@"%@", returnString);

This final NSLog shows that the parameter that was added (username) is completely empty. Is there something I'm missing?

Community
  • 1
  • 1
Mister Mister
  • 488
  • 6
  • 12

2 Answers2

0

I got it to work, here's the new code I used:

for (NSString *param in _params) {
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"%@\r\n", [_params objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
}

I think the only difference is the charset parameter.

Mister Mister
  • 488
  • 6
  • 12
0

My suggestion:

NSString *stringBody = @"";
NSArray *allKeys = parameters.allKeys;
NSArray *allValues = parameters.allValues;

for (int i = 0; i < allKeys.count; i++) {
    NSString *key = allKeys[i];
    NSString *value = allValues[i];
    stringBody = [stringBody stringByAppendingString:[NSString stringWithFormat:@"%@=%@&", key, value]];
}

Complete code:

FOUNDATION_EXPORT NSString *GetWebServiceResponseHTTP_POST(NSString *url, NSString *webServiceName, NSMutableDictionary *parameters) {
    NSString *stringBody = @"";
    NSArray *allKeys = parameters.allKeys;
    NSArray *allValues = parameters.allValues;

    for (int i = 0; i < allKeys.count; i++) {
        NSString *key = allKeys[i];
        NSString *value = allValues[i];
        stringBody = [stringBody stringByAppendingString:[NSString stringWithFormat:@"%@=%@&", key, value]];
    }

    NSURL *completeURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", url, webServiceName]];
    NSData *body = [stringBody dataUsingEncoding:NSUTF8StringEncoding];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:completeURL];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"some value" forHTTPHeaderField:@"some header"];
    [request setHTTPBody:body];
    [request setValue:[NSString stringWithFormat:@"%d", body.length] forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

    NSError *error = [[NSError alloc] init];
    NSHTTPURLResponse *responseCode = nil;
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&responseCode error:&error];

    if (responseCode.statusCode != 200) {
        NSLog(@"Error getting %@, HTTP status code %i", url, responseCode.statusCode);
        return nil;
    }

    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];    
    return responseString;
}
Idan Moshe
  • 1,955
  • 4
  • 26
  • 64