iPhone POST request truncates string - iphone

When I try to send out a POST request to a specific site the string I try to send gets cut off. When I check the length of the string in Xcode it is about 55000 characters long. The amount of characters received on the site is about 4500.
This is my code:
-(IBAction)convert {
NSString *rosterText = [webView stringByEvaluatingJavaScriptFromString:#"document.documentElement.innerHTML"];
NSString *params = [[NSString alloc] initWithFormat:#"roster=%#", rosterText];
NSString *paramsLength = [NSString stringWithFormat:#"%d", [params length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:#"http://www.thesite/index.php"]];
[request setHTTPMethod:#"POST"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-type"];
[request setValue:paramsLength forHTTPHeaderField:#"Content-Length"];
[request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
NSLog(#"length = %#",paramsLength);
[webView loadRequest:request];
[params release];
[request release];
}
I have a form on the site where you can manually insert the string and that all works fine, so seems to me something is going wrong within the encoding of the POST data.
Anybody know whats going wrong here? Just can't seem to figure it out!
Thanks a lot!

I am not sure, what is going wrong, but I'd suggest you to use ASIHTTPRequest.
ASIHTTPRequest is an easy to use
wrapper around the CFNetwork API that
makes some of the more tedious aspects
of communicating with web servers
easier. It is written in Objective-C
and works in both Mac OS X and iPhone
applications.

Related

NSMutableurlrequest http headerparameter containing newline gets lost

as the title says, can't get it to work...
Nothing strange about the code:
NSMutableURLRequest *request = [self prepareRequest:inUrl];
[request addValue:[NSString stringWithFormat:#"%#", note] forHTTPHeaderField:NOTE];
....
[[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];
So, this all works fine, and it arrives at my tomcat java spring service fine. EXCEPT for if the "note" string contains newline characters! looks fine when i debuglog in the client, but on my server the parameter is NULL.
Not sure if i need to urlencode or escape something?
Anybody have any pointers? Thankful for input.
Make sure you use correct mode when you send data to the server.
[request setHTTPMethod:#"POST"];
The default HTTP method is “GET”
Update:
Here is how I would generally send data to the server. The parameters may vary depending on your content type.
NSData* data = [bodyString NSUTF8StringEncoding];
[request setHTTPMethod:#"POST"];
[request setHTTPBody:data];
[request setValue:[NSString stringWithFormat:#"%d", [data length]] forHTTPHeaderField:#"Content-Length"];
[request setValue:#"text/xml" forHTTPHeaderField:#"Content-Type"];

How to send login and password to a server using ASIHTTPRequest?

I'm pretty new to everything that deals with http and stuff like that and i know almost nothing about how http methods work so i need your advice. In my app i have to send login and password to a server and get a response. By now i'm trying to write a method (it's called STUB in English i believe) which will work in a general way without communicating to actual server. Should i use POST method in a manner similar to:
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:some_server_url];
[request addPostValue:passwordString forKey:loginString];
or something else? if first how do i receive the response from the server? Could you please show me few lines of code?
You can pass login details to server using HTTP POST method like this :
-(NSDictionary*)userLogIn:(NSMutableDictionary*)inputDictionary{
SBJSON *parser = [[SBJSON alloc] init];
NSString *convertedString=[NSString stringWithFormat:#"login=%#&password=%#",username, password];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"Give the url here"]];
[request setHTTPBody:[convertedString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setValue:#"application/json" forHTTPHeaderField:#"Accept"];
[request setValue:#"XXXX" forHTTPHeaderField:#"USER_AGENT"];
[request setHTTPMethod:#"POST"];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSASCIIStringEncoding];
NSDictionary *statuses = [parser objectWithString:json_string error:nil];
[parser release];
[json_string release];
return statuses;
}
This is a sample code and you have to change according to your requirements.

Sending JSON Request to server?

I have following JSON for sending request on server
{
"name":"Home",
"latitude":"45.5037078163837",
"longitude":"-122.622699737549",
"radius":"240"
}
and URL of for request is
URL: https://api.geoloqi.com/1/place/create
I am making request like this,
NSString *urlString= #"https://api.geoloqi.com/1/place/create ";
NSURL* url = [[NSURL alloc] initWithString:urlString];
NSString *jsonRequest = #"{\"name\":\"veer\",\"latitude\":\"45.5037078163837\",\"longitude\":\"-122.622699737549\,\"radius\":\"500\ }";
jsonRequest = [self JSONString:jsonRequest];
NSData* requestData = [jsonRequest dataUsingEncoding:NSUTF8StringEncoding];
NSString* requestDataLengthString = [[NSString alloc] initWithFormat:#"%d", [requestData length]];
NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:#"POST"];
[request setHTTPBody:requestData];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[request setValue:requestDataLengthString forHTTPHeaderField:#"Content-Length"];
[request setTimeoutInterval:30.0];
[url release];
[requestData release];
[requestDataLengthString release];
NSURLConnection *m_URLConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[request release];
What is wrong with this request ?
Your JSON string seems to be missing 2 quotes after the longitude and radius values:
Change
NSString *jsonRequest = #"{\"name\":\"veer\",\"latitude\":\"45.5037078163837\",\"longitude\":\"-122.622699737549\,\"radius\":\"500\ }";
To this
NSString *jsonRequest = #"{\"name\":\"veer\",\"latitude\":\"45.5037078163837\",\"longitude\":\"-122.622699737549\",\"radius\":\"500\" }";
It is not clear from your question what is your issue with that.
Anyway, are you doing also:
[m_URLConnection start];
after creating the connection?
Alternatively, you could create the connection using
[– initWithRequest:delegate:startImmediately:][1]
which allows you to specify that the loading of data shall start immediately.
Also consider using [+sendAsynchronousRequest:queue:completionHandler:][2] as a convenience constructor for your connection.
The Webservice URL you are trying to hit is seem to be using SSL. You may need to put access token in request header in order to get the proper response from web service.
Please see the link for authentication:
https://developers.geoloqi.com/api/authentication

JSON touch iphone-sdk, send data over request

people,
I'm using JSON touch inmy iphone app.
Now I have to send a string and then an array to server, how can I do this?
I get data from json requests succefully, but I have to send some data.
Here is the code I've got so far:
-(void)subimtSelection:(int)aNumber
{
NSString *choiceData=[NSString stringWithFormat:#"%d", aNumber];
NSError *theError=nil;
[[CJSONSerializer serializer] serializeString:choiceData error:&theError];
NSDictionary *jsDic=[NSDictionary dictionaryWithObject:choiceData
forKey:#"selection"];
//WHAT SHOULD I DO NEXT?
}
You can use ASIHTTRequest to POST string/json data to server:
ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:#"http://server.url"];
[request addRequestHeader:#"Accept" value:#"application/json"];
[request addRequestHeader:#"Content-Type" value:#"application/json"];
[request setRequestMethod:#"POST"];
[request appendPostData:[yourJSONString dataUsingEncoding:NSUTF8StringEncoding]];
[request startSynchronous];
If you want to post a string value then try:
[request appendPostData:#"key=value"];
ASIHTTPRequest can be used in asynchronious mode as well.
P.S. I did not tested the code, but it should work.
To post form data you can use ASIFormDataRequest, documentation is here

iPhone Application button with URL POST method

I would like to ask about the button and the url post method in iPhone application.
In my program, I want the user to click a button, and then a url will be called by POST method. For the url, it may need to redirect to somewhere (302 or 303) etc and final is 200.
I have complete the button and the success page, however, I don't know how to use the objective-C library. I found lots of reference of this forum, but I don't understand what the code means. Can anyone help me?
The following is a question which I believe related to the question.
Invoking a http post URL from iphone using .net web service
Thank you very much.
If you're okay with blocking the thread you're making the request on, this is just about as simple as it gets.
NSString *postBody = [NSString stringWithFormat:#"param1=%#&param2", param1value, param2value];
NSData *postData = [postBody dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:targetURL];
[request setHTTPMethod:#"POST"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setHTTPBody:postData];
NSURLResponse *response = nil;
NSError *error = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
A few caveats:
This will perform a synchronous (blocking) request, so either do it on a background thread or look into using the NSURLConnection delegate methods.
POST data must be URL-encoded, so you may need to do some preprocessing of your parameter values.
Redirects will occur automatically until a 200 OK or an error is encountered.