AFNetworking 2 - Image upload - Request failed: Media type not supported (415)

I know this question has been answered multiple times, but I tried all the answers with no luck. I don’t think I’m doing something fundamentally wrong, but something is not so clear.

I am using the following code to upload PNG files to tinypng.com . As far as I can see the download itself is working, but I am getting the error: Request error: unsupported media type (415)

The images I use are downloaded as JPEG, then resized and converted to PNG format. The saved files are in order. Now I want to send them to the TinyPNG API before I save them to disk.

If anyone has an idea what happened or have experience with this service, please let me know. Thanks in advance!

Detailed error message

Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: unsupported media type (415)" 
UserInfo=0x6000000e4900 {
    com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x600000220200> { URL: https://api.tinypng.com/shrink } 
    { 
        status code: 415, headers {
            Connection = "keep-alive";
            "Content-Length" = 77;
            "Content-Type" = "application/json; charset=utf-8";
            Date = "Tue, 16 Dec 2014 20:24:16 GMT";
            Server = "Apache/2";
            "Strict-Transport-Security" = "max-age=31536000";
            "X-Powered-By" = "Voormedia (voormedia.com/jobs)";
        } 
    }, 
    NSErrorFailingURLKey=https://api.tinypng.com/shrink,
    com.alamofire.serialization.response.error.data=<7b226572 726f7222 3a224261 64536967 6e617475 
    7265222c 226d6573 73616765 223a2244 6f657320 6e6f7420 61707065 61722074 6f206265 20612050 
    4e47206f 72204a50 45472066 696c6522 7d>, 
    NSLocalizedDescription=Request failed: unsupported media type (415)
}

      

The code I am using

-(void) uploadImage:(NSImage *)image {

    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:TINY_PNG_URL]];

    CGImageRef cgRef = [image CGImageForProposedRect:NULL
                                             context:nil
                                               hints:nil];
    NSBitmapImageRep *newRep = [[NSBitmapImageRep alloc] initWithCGImage:cgRef];
    [newRep setSize:[image size]];   // if you want the same resolution
    NSData *imageData = [newRep representationUsingType:NSPNGFileType properties:nil];

    NSDictionary *parameters = @{@"username": USERNAME, @"password" : PASSWORD};

    AFHTTPRequestOperation *operation = [manager POST:@"" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

        //append image
        [formData appendPartWithFileData:imageData
                                    name:@"filename"
                                fileName:@"photo.png"
                                mimeType:@"image/png"];

    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject);

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@ ***** %@", operation.responseString, error);
    }];

    [operation start];
} 

      

+3


source to share


1 answer


Supported by Mattijs from TinyPNG, it works for me! Thanks Mattijs!

The problem was that the TinyPNG API expects the request body to be only image data, which is not the cumulative part of the form data that I used in my original code.



My working solution:

-(void) uploadImage:(NSImage *)image {

    NSData *imageData = [self PNGRepresentationOfImage:image];

    NSURL *url = [NSURL URLWithString:TINY_PNG_URL];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:imageData];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"image/png" forHTTPHeaderField:@"Content-Type"];

    NSString *authStr = [NSString stringWithFormat:@"%@:%@", USERNAME, PASSWORD];
    NSData *authData = [authStr dataUsingEncoding:NSASCIIStringEncoding];
    NSString *authValue = [NSString stringWithFormat:@"Basic %@", [authData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed]];
    [request setValue:authValue forHTTPHeaderField:@"Authorization"];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    operation.responseSerializer = [AFJSONResponseSerializer serializer];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id result) {
        NSLog(@"Success: %@ ***** %@", operation.responseString, result);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@ ***** %@", operation.responseString, error);
    }];
    [operation start];
}

      

+5


source







All Articles