Upload Image with dictionary - ios

I want to upload image to server from iphone. Using ASIHttpRequest and ASIFormDataRequest i have sucessfully uplaoded picture to server by
[request setdata:...]
But I also want to send userId to server with image. How to achieve this? Here is my code...
ASIFormDataRequest *_request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:URLSaveImage]];
__unsafe_unretained ASIFormDataRequest *request = _request;
[ASIHTTPRequest setShouldThrottleBandwidthForWWAN:YES];
[request setUploadProgressDelegate:self];
NSData *data=UIImageJPEGRepresentation(proImage.image,1.0);
[request setData:data
withFileName:#"profile.jpg"
andContentType:#"image/jpeg"
forKey:#"file"];

For POST method, use setPostValue: forKey: with the setdata:. You dont need to do any additional work . Just post the variable along with image.
[request setPostValue:VALUE forKey:KEY];
[request setData:data withFileName:#"profile.jpg" andContentType:#"image/jpeg"
forKey:#"file"];

Related

How to upload String and Image using ASIFormDataRequest?

I want to upload follow Log Strings along with UIImage,
HOw can I achieve such objective????
I can see to send only image , but i want to send image along with text data using this ASIFormDataRequest, using POST request type.
NSLog(#"data is here %#\n%#\n%#\n%#\n%#\n",deviceID, postID, name, message, picture);
UIImage *image= [UIImage imageNamed:#"profile_avatar.png"];
NSString *strURL = #"https://abc.abc.com/comments/create";
// ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:strURL]];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:strURL]]; // Upload a file on disk
// NSString *filename1=[NSString stringWithFormat:#"friendship.jpg"];
UIImage *image1=image;
NSData *imageData1=UIImageJPEGRepresentation(image1, 1.0);
[request setData:imageData1 withFileName:#"filename" andContentType:#"image/png" forKey:#"avatar"];
[request setRequestMethod:#"POST"];
[request addRequestHeader:#"Authorization" value:[NSString stringWithFormat:#"Basic %#",[ASIHTTPRequest base64forData:[[NSString stringWithFormat:#"username123:pass123"] dataUsingEncoding:NSUTF8StringEncoding]]]];
[request setValidatesSecureCertificate:NO];
//[request appendPostData:body];
[request setDelegate:self];
[request setTimeOutSeconds:3.0];
request.shouldAttemptPersistentConnection = NO;
[request setDidFinishSelector:#selector(uploadRequestFinished:)];
[request setDidFailSelector:#selector(uploadRequestFailed:)];
[request startAsynchronous];
Thanks
There is one method available to send parameter as well
[request setPostValue:#"YourValue" forKey:#"YourKey"];

ASIHttpRequest: Can someone see the URL being sent?

I'm using ASIHttpRequest to send data back and forth to the server in my IOS game.
I'm wondering if someone can see the url being send to the server, someway?
... If that's the case, cheating is possible.
thanks in advance
The request looks like this:
NSURL *url = [NSURL URLWithString: #"http://xx.xxx.xx.x/development/"];
__block ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL: url];
__weak ASIHTTPRequest *request_b = request;
[request setDelegate: self];
[request setQueue: [[DatabaseHelper sharedInstance] networkQue]];
[request setShouldContinueWhenAppEntersBackground: NO];
[request addRequestHeader:#"Content-Type" value:#"text/html; charset=utf-8;"];
[request setDefaultResponseEncoding:NSUTF8StringEncoding];
[request setTimeOutSeconds: 20.0f];
[request setCachePolicy: ASIDoNotWriteToCacheCachePolicy | ASIDoNotReadFromCacheCachePolicy];
[request setPostValue:#"aSecretString" forKey:#"secret"];
[request setPostValue: #"updateUser" forKey:#"apiToRun"];
[request setPostValue: #"4" forKey:#"userId"];
url:
http://xx.xxx.xx.x/development/secret=aSecretString&apiToRun=updateUser&userId=4
With a GET request, data is sent as part of the URL and is easy to intercept with HTTP monitoring. If you use POST, this information will not be visible in the URL.

IOS: ASIFormDataRequest with quotation marks

I have to send json to a web service that only accepts it via a POST variable.
ASIFormDataRequest insists on escaping my quotation marks.
any help would be appreciated
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
NSString *body = [NSString stringWithFormat:#"{\"user\":\"username\",\"pass\":\"password\"}"];
[request setPostValue:body forKey:#"body"];
[request startSynchronous];
output: "{\"user\":\"username\",\"pass\":\"password\"}"
Try Converting the parameter into JSON representation before sending it through SBJSON or whatever JSON parser you are using.
JosephH is right
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request appendPostData:[dataString dataUsingEncoding:NSUTF8StringEncoding]];
[request setRequestMethod:#"POST"];
[request startSynchronous];
was the way to go.

Uploading .db file to server..disk image malformed?

Can some please help in understanding what is going wrong???
I will have upload my .db file to server using multipart/formdata request.
I'm using ASIFormdataRequest to form the request and i'm getting the response as success.But when I try to download the file I'm getting disk image malformed.
I'm pasting the code
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
NSString* path = [self FilePath];
NSData *dataBaseData = [NSData alloc];
dataBaseData = [NSData dataWithContentsOfFile:path];
NSLog(#"databaselength %d",dataBaseData.length);
NSString* authHeader = [self returnAuthHeader];
NSLog(#"Auth Header %#", authHeader);
[request addRequestHeader:#"Accept" value:#"application/xml"];
[request addRequestHeader:#"Authorization" value:authHeader];
[request addRequestHeader:#"Content-Type" value:#"multipart/form-data"];
// [request setFile:dataBaseData withFileName:#"sample.db" andContentType:#"multipart/form-data" forKey:#"filedata"];
[request addData:dataBaseData withFileName:#"sample.db" andContentType:#"application/octet-stream" forKey:#"filedata"];
[request setPostFormat:ASIMultipartFormDataPostFormat];
[request setDelegate:self];
NSLog(#"Request %#",request);
[request updateUploadProgress];
[request startAsynchronous];
Found why db got corrupted and gave Disk image malformed!!!!!!
Was reading the db in a file in one thread and inserting values to the tables in the db in another thread.So the integrity of the db was not maintained.

ASIHTTPRequest POST for basecamp

Hi I am using ASIHTTPRequest POST for adding time entry on basecamp for logged in user.
But
- (void)request:(ASIHTTPRequest *)request didReceiveResponseHeaders:(NSDictionary *)responseHeaders
method of ASIHTTPRequest is getting called and in
- (void)requestFailed:(ASIHTTPRequest *)request
I am getting error as access denied.
I also need to send aunthentication token to basecamp URl please help me how to do this.
NSString *postData=[NSString stringWithFormat: #"<time-entry>\
<person-id> 898989 </person-id>\
<date>2011-04-07</date>\
<hours>2:00</hours>\
<description>test</description>\
</time-entry>"];
NSURL *url = [NSURL URLWithString:#"https://test.basecamphq.com/projects/4644/time_entries.xml"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setDelegate:self];
[request setRequestMethod:#"POST"];
[request appendPostData:[postData dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]];
[request addRequestHeader:#"Accept" value:#"application/xml"];
[request addRequestHeader:#"Content-Type" value:#"application/xml"];
[request authenticationNeeded];
[request setValidatesSecureCertificate:NO];
[request startAsynchronous];
This is my code. Please help.
Assuming this is the right API doc:
http://developer.37signals.com/basecamp/
Then basecamp using HTTP basic authentication, meaning you need to do this:
[request setAuthenticationScheme:(NSString *)kCFHTTPAuthenticationSchemeBasic];
[request setUsername:yourApiToken];
[request setPassword:#"X"];
Putting your API token where it says 'yourApiToken'.

Resources