By fi12

2018-02-23 02:20:38 8 Comments

I'm trying to upload an image to an API and then use the result. When using Postman, everything runs successfully. When I used the export feature to convert the code to Objective-C, I tried running it, but I'm getting a status code 400 (my parameters are invalid).

Here is the Postman request: enter image description here

enter image description here

Here is my own code:

    // Get image name
    NSURL *refURL = [info valueForKey:UIImagePickerControllerReferenceURL];
    PHFetchResult *result = [PHAsset fetchAssetsWithALAssetURLs:@[refURL] options:nil];
    NSString *filename = [[result firstObject] filename];

    // Get image file path and append file name onto it
    NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
    NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:filename];
    [imageData writeToFile:path atomically:YES];

    NSDictionary *headers = @{ @"content-type": @"multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
                               @"Content-Type": @"multipart/form-data",
                               @"Accept": @"application/json",
                               @"Authorization": @"Token 60f6be2a21bdf731d86a8817b440a1afba692fed",
                               @"Cache-Control": @"no-cache",
                               @"Postman-Token": @"7d262730-0c7d-66dc-bbbb-43f40dbfe8ce" };
    NSArray *parameters = @[ @{ @"name": @"task", @"value": @"dc9ef71a-b8a0-4a12-90fd-83d717cf887f" },
                             @{ @"name": @"image_file", @"fileName": path } ];
    NSString *boundary = @"----WebKitFormBoundary7MA4YWxkTrZu0gW";    

    NSError *error;
    NSMutableString *body = [NSMutableString string];
    for (NSDictionary *param in parameters) {
        [body appendFormat:@"--%@\r\n", boundary];
        if (param[@"fileName"]) {
            [body appendFormat:@"Content-Disposition:form-data; name=\"%@\"; filename=\"%@\"\r\n", param[@"name"], param[@"fileName"]];
            [body appendFormat:@"Content-Type: %@\r\n\r\n", param[@"contentType"]];
            [body appendFormat:@"%@", [NSString stringWithContentsOfFile:param[@"fileName"] encoding:NSASCIIStringEncoding error:&error]]; 

            if (error) {
                NSLog(@"%@", error);
        } else {
            [body appendFormat:@"Content-Disposition:form-data; name=\"%@\"\r\n\r\n", param[@"name"]];
            [body appendFormat:@"%@", param[@"value"]];
    [body appendFormat:@"\r\n--%@--\r\n", boundary];
    NSData *postData = [body dataUsingEncoding:NSASCIIStringEncoding]; 

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@""]
    [request setHTTPMethod:@"POST"];
    [request setAllHTTPHeaderFields:headers];
    [request setHTTPBody:postData];

    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
                                                completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                                    if (error) {
                                                        NSLog(@"%@", error);
                                                    } else {
                                                        NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
                                                        NSLog(@"%@", httpResponse);
    [dataTask resume];

(It's not shown here, but I'm trying to use an image that the user will select from an image picker.) Why am I getting a status code 400 and how can I get the correct result?

Related Questions

Sponsored Content

Sponsored Content