Domanda

I have an iOS application which makes a authenticated POST request to services online including Facebook. My problem is that I know how to POST text, but I can't post images. Here is my code:

 NSData *imageData = UIImagePNGRepresentation(image_selected);
    NSString *postLength = [NSString stringWithFormat:@"%d", [imageData length]];

    // Init the URLRequest
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setHTTPMethod:@"POST"];

    NSString *facebook_picture_url = [NSString stringWithFormat:@"https://graph.facebook.com/ID/photos?message=%@&access_token=%@", inputted_text, token_facebook];
                    [request setURL:[NSURL URLWithString:facebook_picture_url]];
                    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
                    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
                    [request setHTTPBody:imageData];
                    request.HTTPBody = imageData;
                    request.HTTPMethod = @"POST";


                    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
                    NSHTTPURLResponse *response = nil;
                    NSError *error = nil;
                    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
                    NSLog(@"The responce:\n\n%@", response);

                    if (connection) {
                        // response data of the request
                    }

As you can see I can add the 'imageData' to the HTTPBody but the problem is that some services want it as some sort of parameter. So for example: Facebook wants it to be:

source="imagedata...."

Aslo if you have a look at the URL, you will see there is a 'message=' parameter. And that all works, but surely I could add that to the HTTPBody??

Do I need to use something like a NSDictionary??

Thank you for your time, Daniel

È stato utile?

Soluzione

Thanks to the developer community for their help lol.... Anyway, if anyone is reading this and is interested to know how you can POST an image to a service such as Facebook, this is how I did it:

NSData *imageData = UIImagePNGRepresentation(image_selected);

NSString *facebook_picture_url = [NSString stringWithFormat:@"https://graph.facebook.com/USER_ID/photos?message=%@&access_token=%@", inputted_text, token_facebook];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:facebook_picture_url]];
[request setHTTPMethod:@"POST"];

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

NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\".JPG\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];

NSHTTPURLResponse *response = nil;
NSError *error = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(@"The responce:\n\n%@", responseData);

if (error == nil && response.statusCode == 200) {
    NSLog(@"%li", (long)response.statusCode);
}

else {
  //Error handling
  NSLog(@"%@", response);
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top