I am struggling for hours to get AFMultipartFormData to work...
I need to signup a user by sending his info and his picture.
The documentation i have from the back-end developer to achieve this is:
The required parts are: 'registration' with content type 'application/json' and 'pic' with content type image/*
I tried several ways to do this without any success. What is wrong with the code below?
-(void)serverAuthentication {
NSDictionary *jsonDictionary = #{#"clientId":#"someClientId3", #"name":#"Pantelis", #"email":#"someuser#gmail.com", #"password":#"1234567"};
NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:#"sampleJohnTsioris"], 0.5);
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary options:0 error:&error];
if (!jsonData) {
NSLog(#"JSON ERROR");
} else {
NSString *JSONString = [[NSString alloc] initWithBytes:[jsonData bytes] length:[jsonData length] encoding:NSUTF8StringEncoding];
NSLog(#"JSON OUTPUT: %#",JSONString);
}
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.requestSerializer setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[manager POST:#"http://someserver.com/auth/account/email" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFormData:jsonData name:#"registration"];
[formData appendPartWithFileData:imageData name:#"pic" fileName:#"photo.jpg" mimeType:#"image/*"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"operation=%#",operation);
NSLog(#"Response: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"operation=%#",operation);
NSLog(#"Error: %#", error);
}];
}
The error i get:
Error Domain=com.alamofire.error.serialization.response Code=-1011
"Request failed: unsupported media type (415)"
You can't change the content type of the request. It must be "multipart/form-data".
You need to make sure the "registration" part has a mime type of "application/json".
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:#"http://someserver.com/auth/account/email" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:jsonData name:#"registration" fileName:#"user.json" mimeType:#"application/json"];
[formData appendPartWithFileData:imageData name:#"pic" fileName:#"photo.jpg" mimeType:#"image/jpeg"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"operation=%#",operation);
NSLog(#"Response: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"operation=%#",operation);
NSLog(#"Error: %#", error);
}];
I found out the solution. As Jeffery Thomas wrote, you can't change the content type of the request, and there was the problem. Specifically, there was no problem at all sending the image but the json object which needed the name "registration" AND the content-type and i couldn't find the proper way to pass both. So, I made up the headers needed to do that (pass the content-type and the name).
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:#"http://vext.dsigned.gr:8080/auth/account/email" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
NSMutableDictionary *headers = [NSMutableDictionary dictionaryWithObjectsAndKeys: #"application/json; charset=UTF-8",#"Content-Type", #"form-data; name=\"registration\"",#"Content-Disposition", nil];
[formData appendPartWithHeaders:headers body:jsonData];
[formData appendPartWithFileData:imageData name:#"pic" fileName:#"avatar.png" mimeType:#"image/png"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"operation=%#",operation);
NSLog(#"Response: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"operation=%#",operation);
NSLog(#"Error: %#", error);
}];
Related
I am trying to send a base64 encoded image among some other strings using http POST and AFNetworking. I am trying to send the parameters as an NSDictionary:
NSMutableDictionary *info = [NSMutableDictionary dictionary];
[info setValue:filedata forKey:#"filedata"];
[info setValue:comments forKey:#"comments"];
[info setValue:username forKey:#"username"];
[info setValue:#"jpg" forKey:#"filetype"];
[info setValue:filename forKey:#"filename"];
and POST using AFNetworking:
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[manager POST:#"https://myurl.com/fileupload" parameters:info progress:nil success:^(NSURLSessionTask *task, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
However I am not sure what else to do at this point. What am I missing? Thank you
You can use the following example
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager POST:#"https://myurl.com/fileupload" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:imageData
name:#"key name for the image"
fileName:photoName mimeType:#"image/jpeg"];
} success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(#"Response: %#", responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(#"Error: %#", error);
}];
I am sending multipart data to server text along with images and voice/image and voice are optional in this case when i am not sending the image data or voice the app is crashing please help on this !
-(void)uploadphoto{
NSString* mid= #"1";
NSString*userid=#"13"; //[[NSUserDefaults standardUserDefaults] valueForKey:kUserID];
imageData = UIImagePNGRepresentation (thumbnail.image);
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:baseURLString]];
NSDictionary *parameters = #{#"UserID":userid, #"Name": name_TF.text,#"MandalID":mid,#"Address":address_TV.text,#"PinCode":pincode_TF.text,#"Email":emailid_TF.text,#"Dese":grivence_TV.text};
AFHTTPRequestOperation *op = [manager POST:#"Grievance/CreateRequest" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:imageData name:#"file" fileName:#"image.png" mimeType:#"image/png"];
[formData appendPartWithFileData:audioData name:#"file" fileName:#"Audio.m4a" mimeType:#"audio/.mp4 .m4a"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"Success: %# ***** %#", operation.responseString, responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %# ***** %#", operation.responseString, error);
}];
[op start];
}
}
AFHTTPRequestOperation *op = [manager POST:#"Grievance/CreateRequest" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
if (imageData!=nil)
[formData appendPartWithFileData:imageData name:#"file" fileName:#"image.png" mimeType:#"image/png"];
if (audioData!=nil)
[formData appendPartWithFileData:audioData name:#"file" fileName:#"Audio.m4a" mimeType:#"audio/.mp4 .m4a"];
} success:^ ...
In case of while you not send image or audio your imagedata & audiodata contains nil ( you can't send image or audio which have nil data).
Set bool according to your request
-(void)uploadphoto{
BOOL isImgData = YES; //set according to avaibility
BOOL isVoiceData = YES;//set according to avaibility
NSString* mid= #"1";
NSString*userid=#"13"; //[[NSUserDefaults standardUserDefaults] valueForKey:kUserID];
imageData = UIImagePNGRepresentation (thumbnail.image);
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:baseURLString]];
NSDictionary *parameters = #{#"UserID":userid, #"Name": name_TF.text,#"MandalID":mid,#"Address":address_TV.text,#"PinCode":pincode_TF.text,#"Email":emailid_TF.text,#"Dese":grivence_TV.text};
AFHTTPRequestOperation *op = [manager POST:#"Grievance/CreateRequest" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
if(isImgData){
[formData appendPartWithFileData:imageData name:#"file" fileName:#"image.png" mimeType:#"image/png"];
}
if(isVoiceData){
[formData appendPartWithFileData:audioData name:#"file" fileName:#"Audio.m4a" mimeType:#"audio/.mp4 .m4a"];
}
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"Success: %# ***** %#", operation.responseString, responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %# ***** %#", operation.responseString, error);
}];
[op start];
}
}
I need to send image as param like
URl : some API
params : {profileImage:string(file)}
Means in param list only i have to send image file as string.
i used the below code. but it is not working.
NSData *dataImage = [[NSData alloc] init];
dataImage = UIImagePNGRepresentation(selectedImage);
NSString *stringImage = [dataImage base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
NSDictionary *params = {profileImage : stringImage}
NSString *url = [NetworkRoutes postProfileImageAPIWithMobileNumber:[PTUserDetails getMobileNumber]];
self.operationManager = [AFHTTPSessionManager manager];
self.operationManager.responseSerializer = [AFJSONResponseSerializer serializer]; //
[self.operationManager.requestSerializer setAuthorizationHeaderFieldWithUsername:#“userName” password:#“some password”];
[self.operationManager POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
NSError *error;
if (![formData appendPartWithFileURL:[NSURL fileURLWithPath:path] name:#"file" fileName:[path lastPathComponent] mimeType:#"image/jpg" error:&error]) {
NSLog(#"error appending part: %#", error);
}
} progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
}];
your answer no need to be in afnetworking , can also be in nsurlconnection
I am getting resposne
{
response :"Please upload image file"
}
OR
Suggest me how to do like in the attached screen shot . In post man i am getting response
NSData *imgData = UIImageJPEGRepresentation(image, 1.0);
NSUInteger fileSize = [imgData length];
if(fileSize>400000)
{
float size = (float)((float)400000/(float)fileSize);
imgData = [NSData dataWithData:UIImageJPEGRepresentation(image, size)];
}
NSString *imgProfilePic = [imgData base64Encoding];
and then you can send this imgProfilePic to Webservice
If you send your image in multipart then this might be helpful and easiest way than BASE64
and also no need to convert your image into BASE64 String.
- (void)uploadImage:(UIImage*)image withParams:(NSDictionary*)paramsDict withURL:(NSString *)URL
{
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
AFHTTPRequestOperationManager *manager =
[AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager POST:URL parameters:paramsDict constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
if (imageData!=nil) {
[formData appendPartWithFileData:imageData name:#"imagename" fileName:#"filename" mimeType:#"image/jpeg"];
}
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"success = %#", responseObject);
[appDelegate dismissLoading];
if ([[responseObject valueForKey:#"code"] isEqualToString:#"200"])
{
// code after success
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[appDelegate dismissLoading];
NSLog(#"error = %#", error);
}];
}
Try to send like following (one of the below) way:
1.
-(void)uploadimage{
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:#"http://your server.url"]];
NSData *imageData = UIImageJPEGRepresentation(self.avatarView.image, 0.5);
// if you want to pass another parameter with image then
NSDictionary *param = #{#"username": self.username, #"password" : self.password};
AFHTTPRequestOperation *operation = [manager POST:#"rest.of.url" parameters:param constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
//do not put image inside parameters dictionary, but append it!
[formData appendPartWithFileData:imageData name:paramNameForImage fileName:#"photo.jpg" mimeType:#"image/jpeg"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"Success: %# ***** %#", operation.responseString, responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %# ***** %#", operation.responseString, error);
}];
[operation start];
}
2.
UIImage *image = [UIImage imageNamed:#"imageName.png"];
NSData *imageData = UIImageJPEGRepresentation(image,1);
NSString *queryStringss = [NSString stringWithFormat:#"http://your server/uploadfile/"];
queryStringss = [queryStringss stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:#"text/html"];
[manager POST:queryStringss parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
{
[formData appendPartWithFileData:imageData name:#"fileName" fileName:#"imageName.png" mimeType:#"image/jpeg"];
}
success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSDictionary *dict = [responseObject objectForKey:#"Result"];
NSLog(#"Success: %# ***** %#", operation.responseString, responseObject);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(#"Error: %# ***** %#", operation.responseString, error);
}];
This is my Code:
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:nil];
NSString *json = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSDictionary *dict = #{#"body":json};
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager POST:#"http://abc/" parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSString *myString = [[NSString alloc] initWithData:operation.request.HTTPBody encoding:NSUTF8StringEncoding];
NSLog(#"Error: %#", myString);
}];
But i am Getting Error Like This
body=%7B%22pincode%22%3A%22gfgfgf%22%2C%22mobile%22%3A%221111%22%2C%22password%22%3A%22rwerew%22%2C%22profile_pic%22%3A%22%22%2C%22social_accesstoken%22%3A%22%22%2C%22sponsor_choice%22%3A3%2C%22sponsor_id%22%3A%22%22%2C%22social_provider%22%3A%22Normal%22%2C%22email%22%3A%22ghffh%22%2C%22gender%22%3A1%2C%22name%22%3A%22narendra%22%2C%22nickname%22%3A1233%2C%22country%22%3A%22Japan%20%28JPY%29%22%2C%22fromLogin%22%3A%22%22%2C%22methodName%22%3A%22signup%22%2C%22birth_date%22%3A%2208-06-01%22%7D
Can any one suggest me on this kind...
There is no need to convert error to encoded string....
First of all clear that.. which type of parameter you needed..
and then try this code for simple posting data:
(Make related changes according to your parameters needed)
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
NSDictionary *param = # {#"body":json};
[manager POST:URL_HERE parameters:param
success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(#"JSON: %#", responseObject);
}
failure:
^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
I'm banging my head against the wall with this one. I want to select UIImage from library and upload it to server, like on could do with <form action="http://blabla.request.cfm" method="post" enctype="multipart/form-data">. Instead of success I got this error:
error = Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x145e5d90 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
I tried this way:
-(void)uploadPhoto{
NSString *path = #"http://blabla.request.cfm";
NSData *imageData = UIImageJPEGRepresentation(self.imageView.image, 0.9);
int priv = self.isPrivate ? 1 : 0;
NSDictionary *parameters = #{#"username": self.username, #"password" : self.password, #"private" : #(priv), #"photo" : imageData};
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:path parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
if(self.imageView.image){
[formData appendPartWithFileData:imageData name:#"avatar" fileName:#"avatar.jpg" mimeType:#"image/jpeg"];
}
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"[UploadVC] success = %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"[UploadVC] error = %#", error);
}];
[self blockView:self.view block:YES];
}
but it's not working... server says that there is no file. Not sure if encrypting is wrong, mime type or what?
Tried also this:
[manager POST:path parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"[UploadVC] success = %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"[UploadVC] error = %#", error);
}];
and this:
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager POST:path parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFormData:imageData name:#"photo"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"[UploadVC] success = %#", responseObject);
[self blockView:self.view block:NO];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"[UploadVC] error response.object = %#", operation.responseObject);
[self blockView:self.view block:NO];
}];
nothing is working. Hope someone can help, 'cause I'm stuck with it and circling from question to question here on SO
tia
EDIT: new attempt
1) first was multi-part form
2) creating upload task
none of them worked for me, so I'm still trying to cope with that, but cannot see any solution
I'm not sure which part (I think that some details were missing) was responsible, but I did it finally :) here you go:
-(void)uploadPhoto{
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:#"http://server.url"]];
NSData *imageData = UIImageJPEGRepresentation(self.avatarView.image, 0.5);
NSDictionary *parameters = #{#"username": self.username, #"password" : self.password};
AFHTTPRequestOperation *op = [manager POST:#"rest.of.url" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
//do not put image inside parameters dictionary as I did, but append it!
[formData appendPartWithFileData:imageData name:paramNameForImage fileName:#"photo.jpg" mimeType:#"image/jpeg"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"Success: %# ***** %#", operation.responseString, responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %# ***** %#", operation.responseString, error);
}];
[op start];
}
Works like a charm :)
You can upload an image with AFNetworking using Swift like this...
let compression = 0.5
let imageData = UIImageJPEGRepresentation("image", CGFloat(compression))
if imageData != nil{
var manager = AFHTTPRequestOperationManager()
manager.responseSerializer.acceptableContentTypes = NSSet(array: ["text/html", "application/json"]) as Set<NSObject>
var dictParams = [
"familyId":"10000",
"contentBody" : "Some body content for the test application",
"name" : "the name/title",
"typeOfContent":"photo"
]
let url = "http://...."
manager.POST(url, parameters: dictParams, constructingBodyWithBlock: { (formData: AFMultipartFormData!) -> Void in
//code
formData.appendPartWithFileData(imageData, name: "file", fileName: "filename", mimeType: "image/png")
}, success: { (operation:AFHTTPRequestOperation!, responseObject:AnyObject!) -> Void in
println(responseObject)
}, failure: { (operation:AFHTTPRequestOperation!, error:NSError!) -> Void in
println(error)
})
}
UIImage *image = [UIImage imageNamed:#"decline_clicked.png"];
NSData *imageData = UIImageJPEGRepresentation(image,1);
NSString *queryStringss = [NSString stringWithFormat:#"http://your server/uploadfile/"];
queryStringss = [queryStringss stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:#"text/html"];
[manager POST:queryStringss parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
{
[formData appendPartWithFileData:imageData name:#"fileName" fileName:#"decline_clicked.png" mimeType:#"image/jpeg"];
}
success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSDictionary *dict = [responseObject objectForKey:#"Result"];
NSLog(#"Success: %# ***** %#", operation.responseString, responseObject);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(#"Error: %# ***** %#", operation.responseString, error);
}];