2017-07-17 88 views
0

到多數據添加到服務器我必須添加一些數據,包括圖像服務器以下細節:我想通過AFNetworking

方法:POST 服務器URL:http://dev.aaaa.com/register

頁眉: 鍵值 的App_id XXXXX APP_KEY YYYYY ENCTYPE的multipart/form-data的

身體:

個鍵值 數據{「代碼」:「21187」,其中「fname」:「和Manish」,「L-NAME」:「夏爾馬」} 圖像mypic.jpeg

的朋友請幫我形成的身體和頭部的請求在AFNetworking下。

感謝大家在考慮這個問題,並協助我

回答

0
UIImage *image = //Your Image. 

NSDictionary *params [email protected]{@"data": @{@"code" : @"21187", 
            @"fname" : @"Manish", 
            @"lname" : @"lname" 
            } 
         }; 


static AFHTTPRequestOperationManager *afPostManager = nil; 

afPostManager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL: [NSURL URLWithString:@"http://dev.aaaa.com"]]; 
[afPostManager.responseSerializer setAcceptableContentTypes:[NSSet setWithObject:@"application/json"]]; 

[afPostManager.requestSerializer setValue:@"xxxx" forHTTPHeaderField:@"app-id"]; 
[afPostManager.requestSerializer setValue:@"xxxx" forHTTPHeaderField:@"app-key"]; 
[afPostManager.requestSerializer setValue:@"multipart/form-data" forHTTPHeaderField:@"enctype"]; 

afPostManager.responseSerializer = [AFJSONResponseSerializer 
            serializerWithReadingOptions:NSJSONReadingAllowFragments]; 


[afPostManager POST:@"register" parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) { 

    NSData *imageData = UIImageJPEGRepresentation(image, 0.5); 

    [formData appendPartWithFileData:imageData name:title fileName:@"mypic.jpg" mimeType:@"image/jpeg"]; 

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

} failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) { 
    NSLog(@"failed : %@",error.localizedDescription); 

}]; 

試試這個

+0

感謝您的回覆......但沒有奏效。它遇到這個錯誤「2017-07-17 14:05:10.021實現[1172:74340]失敗:請求失敗:不可接受的內容類型:文本/純」。我不需要添加像app_id,enctype等httpheaderfields – Jitendra

+0

更改應用程序/ JSON文本/純然,並嘗試和你寫你想發送app_id和app_key在標題這就是爲什麼我補充說,如果你的API是不需要那個頭然後刪除它 – vp2698

+0

我也試過這一個,但它產生相同的錯誤。我做了一些修改也適合其要求,但同樣的問題..郵遞員的快照....它會幫助你瞭解更多鏈接1)https://drive.google.com/file/d/0BweySDAy0UNja1FscGJVVjIwSVE/view?usp=共享鏈接2)https://drive.google.com/file/d/0BweySDAy0UNjNWZ6bnc3d0Rtc3M/view?usp=sharing – Jitendra

0
NSDictionary *dataDict = <data to send>; 
NSError *error = nil; 

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dataDict options:NSJSONWritingPrettyPrinted error:&error]; 
NSString *jsonString = [NSString stringWithFormat:@"%@", [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]]; 
NSLog(@「json = %@", jsonData); 

NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration]; 
NSURL *baseURL = [NSURL URLWithString:@「http://dev.aaaa.com/」]; 
NSLog(@「base URL = %@", baseURL); 

AFHTTPSessionManager *sessionManager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL sessionConfiguration:config]; 
[sessionManager.requestSerializer setTimeoutInterval:500.0]; 
sessionManager.requestSerializer = [AFJSONRequestSerializer serializer]; 

AFJSONResponseSerializer *responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments]; 
sessionManager.responseSerializer = responseSerializer; 

sessionManager.responseSerializer.acceptableContentTypes = [sessionManager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"]; 

NSData *imageData = UIImageJPEGRepresentation(image, 0.5); 

[sessionManager POST:@「register」 parameters:dataDict constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { 

    //-------- here append images as form data ------------------------- 

    [formData appendPartWithFileData:imageData name:@「image」 fileName:@「myimage.jpg" mimeType:@"image/jpeg"]; 

} progress:^(NSProgress * _Nonnull uploadProgress) { 

    progressBlock(uploadProgress); 

} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { 

    successBlock(task, responseObject); 

} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { 

    faliureBlock(task, error); 
}]; 

使用這可能會幫助你。

+0

嗨理查德....有沒有這樣的方法[sessionManager POST:@「register」parameters:dataDict constructBodyWithBlock:^(id for .........並且在快照中你可以看到我並不只是傳遞數據,而是一個圖像旁邊.... – Jitendra