cocoa-touchhttphttp-headersmultipartform-dataasiformdatarequest

ASIFormDataRequest ASIMultipartFormDataPostFormat header formatting


I have problem with the header formatting for multipart message. How to create one header with multiple values for one part.

What I want :

--0xKhTmLbOuNdArY-28217FDC-8163-459B-AB09-3DE87210721D
OBR_SORT_IDX:1
OBR_ENUM:1
Content-Disposition: form-data; name="IMAGE"; filename="image1_0.png"
Content-Type: image/png

<theBinData>

What I have:

--0xKhTmLbOuNdArY-28217FDC-8163-459B-AB09-3DE87210721D
Content-Disposition: form-data; name="OBR_ENUM"

1
--0xKhTmLbOuNdArY-28217FDC-8163-459B-AB09-3DE87210721D
Content-Disposition: form-data; name="OBR_SORT_IDX"

1
--0xKhTmLbOuNdArY-28217FDC-8163-459B-AB09-3DE87210721D
Content-Disposition: form-data; name="IMAGE"; filename="image1_0.png"
Content-Type: image/png

<theBinData>

The code I use for testing:

UIImage *img1 = [UIImage imageNamed:@"alu_texture.png"];
UIImage *img2 = [UIImage imageNamed:@"alu_texture.png"];

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:strUrl]];
[request setPostBodyFilePath:thePath];
request.shouldStreamPostDataFromDisk = YES;

[request setPostFormat:ASIMultipartFormDataPostFormat];
[request setRequestMethod:@"POST"];

NSString *imgName = [NSString stringWithFormat:@"image%d_%d.png", 1, 0];
NSData *data = [NSData dataWithData: UIImagePNGRepresentation(img1)];
[request addPostValue:@"1" forKey:@"OBR_ENUM"];
[request addPostValue:@"0" forKey:@"OBR_SORT_IDX"];
[request addData:data withFileName:imgName andContentType:@"image/png" forKey:@"IMAGE"];

imgName = [NSString stringWithFormat:@"image%d_%d.png", 1, 1];
data = [NSData dataWithData: UIImagePNGRepresentation(img2)];
[request addPostValue:@"1" forKey:@"OBR_ENUM"];
[request addPostValue:@"1" forKey:@"OBR_SORT_IDX"];
[request addData:data withFileName:imgName andContentType:@"image/png" forKey:@"IMAGE"];

[request setDelegate:self];
[request startAsynchronous]

Solution

  • So I edited the method - (void)buildMultipartFormDataPostBody of ASIFormDataRequest. I added new array with dictionaries for the multipart header.

    code for buildMultipartFormDataPostBody in the for loop for fileData:

    NSDictionary *d = [self headerMultyPartData][i];
    NSArray *keys = d.allKeys;
    for(NSString *key in keys)
        [self appendPostString:[NSString stringWithFormat:@"%@: %@\r\n", key, [d valueForKey:key]]];