I work under the application that should allow to post images on users wall. At the beginning he is getting authorised, at this stage everything is ok. But when I am trying to make a post request - nothing is posted. could you just help to solve the next issue. When user select an image and press post button:
- (void) postActionSelected
{
NSString *title = [[self titleTextField] text];
NSString *description = [[self commentTextField] text];
NSData *imageData = UIImageJPEGRepresentation(self.currentImage.image, 0.5);
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[imgurServerManager uploadPhoto:imageData
title:title
description:description
completionBlock:^(NSString *result) {
dispatch_async(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
});
} failureBlock:^(NSURLResponse *response, NSError *error, NSInteger status) {
dispatch_async(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
[[[UIAlertView alloc] initWithTitle:@"Upload Failed"
message:[NSString stringWithFormat:@"%@ (Status code %ld)", [error localizedDescription], (long)status]
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:@"OK", nil] show];
});
}];
});
}
and then in ImGurServerManager
this post should be done:
+ (void)uploadPhoto:(NSData*)imageData
title:(NSString*)title
description:(NSString*)description
completionBlock:(void(^)(NSString* result))completion
failureBlock:(void(^)(NSURLResponse *response, NSError *error, NSInteger status))failureBlock
{
NSAssert(imageData, @"Image data is required");
//NSAssert(access_token, @"Access token is required");
NSString *urlString = @"https://api.imgur.com/3/upload";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSMutableData *requestBody = [[NSMutableData alloc] init];
NSString *boundary = @"---------------------------0983745982375409872438752038475287";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
//[request addValue:[NSString stringWithFormat:@"access_token %@", access_token] forHTTPHeaderField:@"access_token"];
[requestBody appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary]
dataUsingEncoding:NSUTF8StringEncoding]];
[requestBody appendData:[@"Content-Disposition: attachment; name=\"image\"; filename=\".tiff\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[requestBody appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[requestBody appendData:[NSData dataWithData:imageData]];
[requestBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
if (title) {
[requestBody appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[requestBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"title\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[requestBody appendData:[title dataUsingEncoding:NSUTF8StringEncoding]];
[requestBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
}
if (description) {
[requestBody appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[requestBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"description\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[requestBody appendData:[description dataUsingEncoding:NSUTF8StringEncoding]];
[requestBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
}
[requestBody appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary]
dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:requestBody];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
if ([responseDictionary valueForKeyPath:@"data.error"]) {
if (failureBlock) {
if (!error) {
error = [NSError errorWithDomain:@"imguruploader" code:10000 userInfo:@{NSLocalizedFailureReasonErrorKey : [responseDictionary valueForKeyPath:@"data.error"]}];
}
failureBlock(response, error, [[responseDictionary valueForKey:@"status"] intValue]);
}
} else {
if (completion) {
completion([responseDictionary valueForKeyPath:@"data.link"]);
}
}
}];
}
I suppose that the reason here is that I missed some additional parameter, but I am totally lost with it. Thank you in advance.
Finally image is uploaded, main reason for not posting it wasn't defining access_token:
[request setValue:[NSString stringWithFormat:@"Bearer %@", token] forHTTPHeaderField:@"Authorization"];
and deleting Dictionary with parameters, in case of post requests on Imgur- it is not necessary.
(void)uploadPhoto:(NSData*)imageData title:(NSString*)title description:(NSString*)description access_token:(NSString*)token topic:(NSString*) topic completionBlock:(void(^)(NSString* result))completion failureBlock:(void(^)(NSURLResponse *response, NSError *error, NSInteger status))failureBlock { NSAssert(imageData, @"Image data is required");
NSString *urlString = @"https://api.imgur.com/3/image.json";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setValue:[NSString stringWithFormat:@"Bearer %@", token] forHTTPHeaderField:@"Authorization"];
[request setURL:[NSURL URLWithString:urlString]];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setTimeoutInterval:30];
[request setHTTPMethod:@"POST"];
NSMutableData *body = [[NSMutableData alloc] init];
NSString *boundary = @"---------------------------0983745982375409872438752038475287";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
form-data; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
if (imageData) {
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: attachment; name=\"image\"; filename=\".tiff\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
}
if (title) {
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"title\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[title dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
}
if (description) {
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"description\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[description dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
}
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
if ([responseDictionary valueForKeyPath:@"data.error"]) {
if (failureBlock) {
if (!error) {
error = [NSError errorWithDomain:@"imguruploader" code:10000 userInfo:@{NSLocalizedFailureReasonErrorKey : [responseDictionary valueForKeyPath:@"data.error"]}];
}
failureBlock(response, error, [[responseDictionary valueForKey:@"status"] intValue]);
}
} else {
if (completion) {
completion([responseDictionary valueForKeyPath:@"data.link"]);
}
}
}];
}