Reputation: 444
i really need help to create a successful post request.
I'm trying to send user info to server by using NSURLConnection
and i followed this tutorial**
My problem is the post request seems not failed but nothing is requested to server. Here is my code:
-(void)sendMyRequest
{
NSString *myJSONString = [[NSString alloc] initWithData:myJSONData encoding:NSUTF8StringEncoding];
NSData *postData = [myJSONString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:signupBaseURLString]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if(conn)
NSLog(@"connection successful"); //it is what is printed
else
NSLog(@"connection failed");
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"data received"); //it is what is printed
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"connection failed with error %@", error);
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"connection did finish loading"); //it is what is printed
}
According to NSLog
output 'conn' is successful, (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
and (void)connectionDidFinishLoading:(NSURLConnection *)connection
delegate methods are called, error delegate method is not called. That is why i do not know what the error is or whether there is any error and if not, why my request is not delivered. Can you please help me, what i'm doing wrong? Thanks in advance.
** There is a line of code in the tutorial that i've not used: [request setValue:@”application/x-www-form-urlencoded” forHTTPHeaderField:@”Current-Type”];
I did not use because i did not understand setValue
parameter "application/x-www-form-urlencoded". It may be needful, if you can enlighten me what this is about, i'd really appreciate.
Update I converted NSData
object in didReceiveData
like that NSString *myString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
When i print it my output is really weird with that intro "Apache Tomcat/7.0.62 - Error report H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;}...". However at the end of the output it says The server refused this request because the request entity is in a format not supported by the requested resource for the requested method but i'm sure server expects string and i try to post in string format but setHTTPBody method force me to send NSData, so what i'm doing is to convert myJSONString to NSData then send it. Problem may be caused here but i do not know what else to do. I couldn't find a way to send directly NSString. How can i post directly NSString type?
Upvotes: 1
Views: 1018
Reputation: 10407
I'm glad you figured out what was wrong with your data format. That said, there's still a related problem with the code that you should probably fix.
With NSURLConnection and NSURLSession, server-side errors are not necessarily reported as errors. The connection is succeeding because the server is, in fact, returning data, just not the data you were expecting.
To detect these sorts of server-side errors, you should implement connection:didReceiveResponse: and check the HTTP status code returned by the server. If it isn't 200 (OK), handle it in whatever way makes sense for your particular application. This is also your only opportunity to obtain various pieces of info that you might need later, such as HTTP headers values, which are stored in the NSHTTPURLResponse object.
(Of course, if the data you get back isn't in the expected format, handle that as an error, too, even if you got a 200 status code and the request seemed to succeed.)
Upvotes: 1
Reputation: 444
Adding below lines in sendMyRequest
method solved my problem.
[request setValue:@”application/x-www-form-urlencoded” forHTTPHeaderField:@”Current-Type”];
[request setValue:@”application/json” forHTTPHeaderField:@”Content-Type”];
[request setValue:@”application/json” forHTTPHeaderField:@”Accept”];
Upvotes: 0
Reputation: 2077
You have to put some code in info.plist file.
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
You can get more information form below link.
http://iosdevtips.co/post/121756573323/ios-9-xcode-7-http-connect-server-error
Upvotes: 0