将post request body设置为form-data objective-c

时间:2017-03-23 13:18:48

标签: ios objective-c form-data

我正在尝试在帖子请求正文中发送form-data,但该服务返回null,因此我使用的方法是错误的(它是由邮递员生成的)或图像编码是不同的,我将向您展示图像应该如何编码以及我如何通过下面的屏幕截图编码它除了我使用的代码

NSString* imgName = [self randomCode];
    NSString* postFile = [NSString stringWithFormat:@"%@%@.png",imgName,userData.userID];

    NSDictionary *headers = @{ @"content-type": @"multipart/form-data; boundary=----WebKitFormBoundaryAt4JdBdihQo7lrrf",
                               @"authorization": [NSString stringWithFormat:@"Bearer %@",userData.accessToken ],
                               @"moduleid": @"441",
                               @"tabid": @"78",
                               @"cache-control": @"no-cache",
                               @"postman-token": @"1482fe03-076b-aa92-5565-125212592762" };
    NSArray *parameters = @[ @{ @"name": @"folder", @"value": userData.userFolder },
                             @{ @"name": @"filter", @"value": @"bmp,gif,jpeg,jpg,jpe,png" },
                             @{ @"name": @"overwrite", @"value": @"true" },
                             @{ @"name": @"postfile", @"fileName": postFile } ];
    NSString *boundary = @"----WebKitFormBoundaryAt4JdBdihQo7lrrf";

    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", @"image/png"];

            NSString* imgString = [self encodeToBase64String:self.teamImage];
            [body appendFormat:@"%@", imgString];

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

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://172.16.0.216/API/internalservices/fileupload/postfile"]
                                                           cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                       timeoutInterval:10.0];
    [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];


- (NSString *)encodeToBase64String:(UIImage *)image {
    NSData * data = [UIImagePNGRepresentation(image) base64EncodedDataWithOptions:NSDataBase64Encoding64CharacterLineLength];
    return [NSString stringWithUTF8String:[data bytes]];
}

以下是服务器收到时图像的外观: enter image description here 以下是服务器从我这里收到它的方式: enter image description here

请告诉我我做错了什么

感谢

1 个答案:

答案 0 :(得分:0)

我认为问题是服务器不希望图像是base64编码的。为了提交非编码图像,您可以直接构造帖子数据而无需中间NSMutableString对象:

NSMutableData *postData = [NSMutableData data];
for (NSDictionary *param in parameters) {
    [postData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    if (param[@"fileName"]) {
        [postData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", param[@"name"], param[@"fileName"]] dataUsingEncoding:NSUTF8StringEncoding]];
        [postData appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n\r\n", @"image/png"] dataUsingEncoding:NSUTF8StringEncoding]];

        [postData appendData:UIImagePNGRepresentation(self.teamImage)];
    } else {
        [postData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", param[@"name"]] dataUsingEncoding:NSUTF8StringEncoding]];
        [postData appendData:[[NSString stringWithFormat:@"%@\n", param[@"value"]] dataUsingEncoding:NSUTF8StringEncoding]];
    }
}
[postData appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];