ios asihttprequest post string - iphone

I am using ASIHTTPRequest, its working fine for posting dictionaries to the web service in the body
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:#"Ben" forKey:#"first_name"];
but how can i use the
ASIFormDataRequest
to send a single string?
something like
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:token];
no value and key,just a string in the body,
thanks!

If you want to send data via POST but prefer to create the POST body yourself, use appendPostData: or appendPostDataFromFile:. The code goes something like this -
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request appendPostData:[#"This is my data" dataUsingEncoding:NSUTF8StringEncoding]];
[request setRequestMethod:#"POST"];

Related

cURL web services from iphone

I need to fetch data from curl web services from iOS, the client provided me a header name, header value and a base url, I dont know what to do with them, the url isn't giving me anything opening in a browser. They mentioned data is JSON encoded.
Please link me to some library or tutorial on how to call them.
You can use ASIHTTPRequest for cURL as well.
what I have used is:
NSURL *url = [NSURL URLWithString:url];
__weak ASIHTTPRequest* request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
request.shouldPresentCredentialsBeforeChallenge = YES;
[request appendPostData:[JSONString dataUsingEncoding:NSUTF8StringEncoding]];
[request setUsername:username];
[request setPassword:password];
[request setRequestMethod:#"PUT"];
request.timeOutSeconds = 30;
request.validatesSecureCertificate = NO;
[request startAsynchronous];
solved it, had to set NSURLRequest http method and header like
[request setHTTPMethod:#"GET"];
[request setValue:#"HEADER_VALUE" forHTTPHeaderField:#"HEADE_RNAME_"];

How to post xml data in url as request?

I am developing an application in which i want to post
xml data as request but i am not able to post it correctly ,i think.
My request xml data is
<loginRequest><username>101</username></loginRequest>
and my request is as follows :
`NSString *post=#"<loginRequest><username>101</username></loginRequest>";
NSURL *url = [NSURL URLWithString:#"my url"];
__block ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setRequestMethod:#"POST"];
NSMutableData *mutData=[[NSMutableData alloc]init];
[request addRequestHeader:#"Content-Type" value:#"text/xml"];
[request setPostValue:#"test" forKey:#"body"];
[request setCompletionBlock:^{
NSData *data=[request responseData];
NSString *response=[request responseString];
}];
[request setFailedBlock:^{
NSLog(#"Failed");
}];
[request startAsynchronous];
`
Kindly help me with this..
You can check with your server api whether it supports different response type..
Accordingly you can set "Accept" parameter of HTTP request header.
[request addRequestHeader:#"Accept" value:#"application/xml"];

Log into website with ASIHttpRequest

I have a website that i want to log into from my iphone in a iphone app i am making, i followed the documentation on ASIHttpRequests website but all i get from the response string is the html code for the login page , but i get a OK http code, Why is this happening?
Here is my code :
-(IBAction)fetchData:(id)sender
{
NSURL *url = [NSURL URLWithString:#"http://www.rssit.site90.com/login.php"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request shouldPresentCredentialsBeforeChallenge];
[request setRequestMethod:#"POST"];
[request setPostValue:#"" forKey:#"username"];
[request setPostValue:#"" forKey:#"password"];
[request setDelegate:self];
[request startAsynchronous];
//Add finish or failed selector
[request setDidFinishSelector:#selector(requestLoginFinished:)];
[request setDidFailSelector:#selector(requestLoginFailed:)];
}
- (void)requestLoginFinished:(ASIHTTPRequest *)request
{
NSString *yourResponse = [request responseString];
NSLog(#"%#", yourResponse);
}
The form on that page is doing a get, not a post. Your server is probably not expecting POST data, and is looking at query string params instead. Change to
[NSURL URLWithString:#"http://www.rssit.site90.com/login.php?username=YOURUSERNAME&password=YOURPASSWORD"];
and set the requestMethod to GET.

how to send post request with nsurlconnection

I have changed over to NSURLConnection and NSURLRequest delegates for my db connections etc.
I was using the ASIHTTPRequest libraries to do all this stuff but finally decided to move on due to the lack of support for that 3rd party library.
what I am woundering is how do I send post requests to my db like you do with the ASIFormDataRequest as shown below
//This sets up all other request
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setDelegate:self];
[request setValidatesSecureCertificate:NO];
[request setPostValue:#"ClientDataSet.xml" forKey:#"filename"];
[request startSynchronous];
i.e. how to do send the setPostValues with the NSURLRequest class?
any help would be greatly appreciated
Using NSURLRequest is slightly more difficult than utilizing ASIHTTPRequest. You have to build your own post body.
NSData *postBodyData = [NSData dataWithBytes: [postBodyString UTF8String] length:[postBodyString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:yourURL];
[request setHTTPMethod: #"POST"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"content-type"];
[request setHTTPBody:postBodyData];

Problem to post large amount of data to the web server in iphone

I am working in simple application in which I need to post data to the server using webservices. I post string data to the web server, but it gives an error "large amount of binary data will be truncated".
NSString *queryString = [NSString stringWithFormat:#"url/message=%#",strMessage];
queryString = [queryString
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:queryString];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
[req addValue:#"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[req addValue:0 forHTTPHeaderField:#"Content-Length"];
[req setHTTPMethod:#"GET"];
NSURLConnection *conn =
[[NSURLConnection alloc] initWithRequest:req delegate:self];
Any ideas?
Thanks in advance.
Did you try http://allseeing-i.com/ASIHTTPRequest/ ?
You can post any kind of datas. For example to send an image :
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:#"Ben" forKey:#"first_name"];
[request setPostValue:#"Copsey" forKey:#"last_name"];
[request setFile:#"/Users/ben/Desktop/ben.jpg" forKey:#"photo"];
(from http://allseeing-i.com/ASIHTTPRequest/How-to-use)
Good Luck ! :-)