2014-03-06 65 views
0

我試圖使用Bufferapp API爲社交網絡自動創建一些緩衝區,並讓它們在自動時間發佈。使用API​​ URL向Web服務發送圖像

我需要使用他們的API,特別是this method,通過圖像和文本發送我的帖子。我已經算出了幾乎所有的東西,但我想不出用這個API發送圖像的正確方法。

我google了一下,但我似乎無法找到辦法做到這一點。我看到的所有示例都是通過POST發送整個圖像的,但它們似乎沒有使用API​​格式。

換句話說,我怎麼能做到這樣?

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

編輯:我還沒有附加任何的源代碼,因爲我不知道如何解決NSImage中的「轉換」的東西,可以用來和送過來使用API​​ URL,但這裏是代碼我試圖產生:

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. 
}]; 

回答

0

我設法去適應這個存在的問題:

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

用以下代碼(需要緊急批准):

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]; 
相關問題