Seems like by uiimage encoding is altered before POST request - ios

I'm currently working on sending uiimages to my server to be stored on the server. So I get an image from the user gallery and then encode it using Base64 and then send it to my server. However all the images I sent to my server don't show. I'm wondering whether the post request I make with the base64encodedstring alters the encoded string.
Here is what I have done.
//Upload picture to server
NSData *imageData=UIImageJPEGRepresentation(showPic.image, 1.0f);
NSLog(#"The size of the image is %i",[imageData length]);
[Base64 initialize];
NSString *strEncoded = [Base64 encode:imageData];
//NSLog(#"This is the encoded string to be uploaded %#",encodedPic);
prefs=[NSUserDefaults standardUserDefaults];
userid=[prefs stringForKey:#"userid"];
NSLog(#" %# is the userid to be used to upload the pic",userid);
NSString *poster = #"username=";
poster = [poster stringByAppendingString:userid];
poster = [poster stringByAppendingString:#"&image="];
poster = [poster stringByAppendingString:strEncoded];
NSString *post = poster;
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:#"http://www.example.com/server/upload"]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
[[NSURLConnection alloc] initWithRequest:request delegate:self];

Related

How to Send JSON String with Special Charaters in iOS?

I'm new to iOS and i'm using the following code to make API Calls.
-(NSData *)sendDataToServer:(NSString*)url :(NSString*)params
{
NSString *postDataString = [params stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(#"postDataString :%#",postDataString);
NSData *postData = [postDataString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d",[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
NSString *urlReq = [NSString stringWithFormat:#"%#", url];
[request setURL:[NSURL URLWithString:urlReq]];
[request setTimeoutInterval:180];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(#"response in Server: %#",responseString);
return responseData;
}
I'm sending the following string as params to the above method. If I send the following data without special characters, I'm getting the success response.
If i Add any special charters as like (&) with json I'm always getting the invalid response, that is the server always returns null.
So Can anyone please provide any suggestion to get the right response when using json string with Special characters like '&' etc.,
submited_data={"safty_compliance_fields":[{"safty_compliance_id":"641","fieldName":"sc1","fieldType":"editText","fieldValue":"wedgies Ig"},{"safty_compliance_id":"642","fieldName":"sc2","fieldType":"editText","fieldValue":"het &"}],"status_id":"2","product_detail":[{"dynamic_fields_id":"639","fieldName":"p1","fieldType":"editText","fieldValue":"data1"},{"dynamic_fields_id":"640","fieldName":"p2","fieldType":"editText","fieldValue":"data2"}],"inspection_id":"3","second_level":[{"questions":[{"checkListValue":"NO","checkListCommentValue":"Jgkjgjkj","sub_category_id":"452","checkListName":"sl1"},{"checkListValue":"YES","checkListCommentValue":"jk","sub_category_id":"453","checkListName":"sl2"},{"checkListValue":"YES","checkListCommentValue":"gh","sub_category_id":"455","checkListName":"sl3"},{"checkListValue":"YES","checkListCommentValue":"nm","sub_category_id":"456","checkListName":"sl4"}],"title":"sl1","entity_second_level_entry_id":"130"},{"questions":[{"checkListValue":"YES","checkListCommentValue":"Bonn","sub_category_id":"454","checkListName":"s22"}],"title":"s211","entity_second_level_entry_id":"131"}],"comment":"Jgkjgjkj","status":"Ongoing"}
You didn't post the percent encoded string, but the original string.

IOS JSON sending an email from Mandrill

I have an IOS application that I would like to send an email via Mandrill. I have tried to implement this, but its not working and Ive confused myself.
When pressing the button to send an email from the IOS application I log this error message:
{"status":"error","code":-1,"name":"ValidationError","message":"You must specify a key value"}
My code is:
NSString *post = [NSString stringWithFormat:#"{\"key\": \"abcdefg123456\", \"raw_message\": \"From: me#mydomain.com\nTo: me#myotherdomain.com\nSubject: Some Subject\n\nSome content.}"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:#"https://mandrillapp.com/api/1.0/messages/send-raw.json"]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSLog(#"Post: %#", post);
NSURLResponse *response;
NSData *POSTReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
NSString *theReply = [[NSString alloc] initWithBytes:[POSTReply bytes] length:[POSTReply length] encoding: NSASCIIStringEncoding];
NSLog(#"Reply: %#", theReply);
Please let me know where I am going wrong.
It looks you forgot the \" after "content.".
Try to write your "post" variable as follow:
NSString *post = [NSString stringWithFormat:#"{\"key\": \"abcdefg123456\", \"raw_message\": \"From: me#mydomain.com\nTo: me#myotherdomain.com\nSubject: Some Subject\n\nSome content.\"}"];
I hope it helps.

Web services for iOS application with C# using mysql?

I am working on iPhone application with web services.
Using HTTP POST method I tried post the data. But data is saving in database. The response I'm getting is 404 or 400. here C# is used instead of PHP.
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:#""]];
NSString *userUpdate =[NSString stringWithFormat:#"Name=%#,FirstName=%#,DateofBirth=%#,Sex=%#,Country=%#,City=%#,PhoneNumber=%#,IncumbentonPremimum=%#",Name,FirstName,DateofBirth,Sex,Country,City,PhoneNumber,IncumbentonPremimum];
NSData *data1 = [userUpdate dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPMethod:#"POST"];
NSString *postLength = [NSString stringWithFormat:#"%d",[data1 length]];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Current-Type"];
[request setHTTPBody:data1];
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
NSString *resSrt = [[NSString alloc]initWithData:responseData encoding:NSASCIIStringEncoding];
//This is for Response
NSLog(#"got response==%#", resSrt);`

How to use GData framework in iOS app to upload images to Picasa?

In my app, I have an imageView, I want to post the image to Picasa.
I got some suggestion to use GData Framwork. I downloaded it from
code.google.com/p/gdata-objectivec-client/downloads/list
But, when I add the .a file in my Build phases, it is displayed as red. I have tried all the method in SO, as well as other forums.
Is there any other way to upload image to Picasa.
Any help would be appreciated.
I don't have an app to test this with, but I've translated the php-curl solution from this SO question.
NSString *authCode = [NSString stringWithFormat:#"GoogleLogin auth=",#"ACCESS CODE FROM OAUTH2"];
NSString *userId = #"USER ID GOES HERE";
NSString *albumId = #"ALBUM ID GOES HERE";
NSString *albumUrl = [NSString stringWithFormat:#"https://picasaweb.google.com/data/feed/api/user/%#/albumid/%#",userId, albumId];
// To get the data from a PNG file
NSData *postData = UIImagePNGRepresentation([UIImage imageNamed:#"YOUR IMAGE"]);
// To get the data from a JPEG file
//NSData *postData = UIImageJPEGRepresentation([UIImage imageNamed:#"YOUR IMAGE"], 0.9f);
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
// Init and set fields of the URLRequest
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod:#"POST"];
[request setURL:[NSURL URLWithString:[NSString stringWithString:albumUrl]]];
[request setValue:#"image/jpeg" forHTTPHeaderField:#"Content-Type"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"2" forHTTPHeaderField:#"GData-Version"];
[request setValue:authCode forHTTPHeaderField:#"Authorization"];
[request setHTTPBody:postData];
NSURLResponse* rep = nil;
NSError *err = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&rep error:&err];
if (data && !err) {
//Do something with the response
}
Hope this helps.

Crashing when using nsstring to post data

I'm trying to send the device token and device name to my server, although it works with only the token, it crashes if i add the device name.
Is there a better way to do this?
(void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
NSLog(#"My token is: %#", deviceToken);
NSString* systemName=[[UIDevice currentDevice] name];
NSString *temp = [NSString stringWithFormat:systemName, deviceToken];
NSData *postData = [[NSString stringWithFormat:#"token=%#", temp] dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d",[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://#############/push/register.php"]]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setHTTPBody:postData];
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
}
the console outputs
2013-12-31 17:41:59.294 Wellington[31788:60b] My token is: <###### ###### ###### ###### ##### ###### ######>
(lldb)
NEW CODE
-(void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
NSLog(#"My token is: %#", deviceToken);
NSString* systemName=[[UIDevice currentDevice] name];
*** NSData *postData = [[NSString stringWithFormat:#"token=%#", deviceToken] dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSData *postData2 = [[NSString stringWithFormat:#"token=%#", systemName] dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d",[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://*********/register.php"]]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setHTTPBody:postData];
[request setHTTPBody:postData2];
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
}
it is crashing on stared line
Shouldn't you do something like,
NSString *temp = [NSString stringWithFormat:#"%#|%#", systemName, deviceToken];
Pipe delimetered values.
Reason your code isn't working...:
NSString *temp = [NSString stringWithFormat:systemName, deviceToken];
Will give you only systemName variables value. Because systemName variable string doesn't have %#symbol in it to the OS will not add deviceToken value into temp variable.

Resources