Skip to content

Instantly share code, notes, and snippets.

@PaulWoodIII
Created February 8, 2013 09:45
Show Gist options
  • Select an option

  • Save PaulWoodIII/4737758 to your computer and use it in GitHub Desktop.

Select an option

Save PaulWoodIII/4737758 to your computer and use it in GitHub Desktop.
A function to post an image onto ADN This does not work. Trying to figure out why
- (void)postImage{
// Get a ADN Token
NSString *access_token = @"foobar";
NSURL *url = [NSURL URLWithString:@"https://alpha-api.app.net"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
[httpClient setDefaultHeader:@"Authorization" value:[NSString stringWithFormat:@"Bearer %@", access_token]];
NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"bird.jpg"], 0.5);
NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST"
path:@"/stream/0/files"
parameters:nil
constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
[formData appendPartWithFileData:imageData name:@"content" fileName:@"bird.jpg" mimeType:@"image/jpeg"];
}];
AFJSONRequestOperation *operation =
[AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(@"Success :%@", JSON);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"Failure: %@", error);
}];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
}];
[httpClient enqueueHTTPRequestOperation:operation];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment