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.
Related
In my application I need to make a PUT type request. I have followed the below steps
NSString *path = #"http://hostname/api/Account/VerifyUser?applicationToken=72B648C6-B2B7-45ED-BA23-2E8AAA187D2C&emailID=xxx#gmail.com&password=aaaaa";
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:
[NSURL URLWithString:path]];
[request setHTTPMethod:#"PUT"];
[request setValue:[NSString stringWithFormat:#"%d", string.length] forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded;charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:[string dataUsingEncoding:NSUTF8StringEncoding]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
For POST and GET I am getting correct output.
I have searched a lot but i am not getting any solution.Please suggest me any possible solution .
Thanks in advance
Try this
For xml request body
Set [request setValue:#"application/x-www-form-urlencoded;charset=utf-8;application/form-data" forHTTPHeaderField:#"Content-Type"];
OR
For json request body
Set [request setValue:#"application/x-www-form-urlencoded;charset=utf-8;application/json" forHTTPHeaderField:#"Content-Type"];
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"];
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.
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'.
I've managed to get simple text data sent to a server using this code:
NSMutableString *parameterString = [[NSMutableString alloc] initWithString: #""];
[parameterString appendString:#"name=steve&"];
[parameterString appendString:#"surname=jobs&"];
[parameterString appendString:#"age=55"];
NSURL *url = [NSURL URLWithString:#"http://example.come/script/"];
request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];
[request setHTTPMethod:#"POST"];
NSData *parameterData = [parameterString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:parameterData];
With that I can send text form data. But how can I send PNG images as well?
You could just convert the image to an NSData object, then base64 encode it to send it as a parameter.
Base64 encoding examples found here: How do I do base64 encoding on iphone-sdk?
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request addData:imageData withFileName:#"image.png" andContentType:#"image/png" forKey:#"yourParamKey"];