Search code examples
objective-cmacoshttpnsimage

Sending an image to a web service using their API URL


I'm trying to use the Bufferapp API to automatically create some buffers for social networks and have them posted at automated times.

I need to use their API, specifically this method, to send in my posts over with an image and text. I have figured almost all of it out, but I cannot think of the right way to send in images using this API.

I have googled a bit but I can't seem to find a way to do this. All examples I have seen that do this send the entire image over POST, but they don't seem to use an API format.

In other words, how could I achieve something like this?

https://webservice.com/api/send_post?access_token=123&text=huehuehue&image=__

EDIT: I haven't attached any source code because I have no idea how to tackle the "conversion" of NSImage to something that can be used and sent over using an API URL, but here is the code I'm trying to generate:

NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperationWithBlock:^{
    NSURL *create = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.bufferapp.com/1/updates/create.json?access_token=%@", kToken, nil]];
    NSString *text = @"text=%23Anime"; //Encoding so this is #Anime
    NSString *now = @"now=0";

    //Somehow add an NSImage to the http body.
}];

Solution

  • I managed to adapt this existing question:

    Objective C: How to upload image and text using HTTP POST?

    With the following code (which needs to be prettified urgently):

    NSURL *create = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.bufferapp.com/1/updates/create.json?access_token=%@", kToken, nil]];
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:create];
    request.HTTPMethod = @"POST";
    
    NSString *text = @"#Anime\r\n\r\>> Download Mignori: http://lnrs.me/mignori";
    NSString *now = @"0";
    NSString *shorten = @"0";
    
    NSImage *image = [[NSImage alloc] initWithContentsOfFile:self.folders[0]];
    
    NSData *imageData = [NSData dataWithContentsOfFile:self.folders[0]];
    
    NSMutableData *body = [NSMutableData data];
    
    NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
    
    //File
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Disposition: attachment; name=\"media[picture]\"; filename=\".jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:imageData]];
    [body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"text\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:text] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"now\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:now] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"shorten\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:shorten] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"profile_ids[]\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"52e99e2b2846b29e18000020"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    
    [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    
    request.HTTPBody = body;
    
    //return and test
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];