API POST method not saving data in sql server

岁酱吖の 提交于 2019-12-13 02:47:47

问题


Hi I'm new to iphone development, I'm currently working with a project where I have a screen, in which user should enter their details, like username and password. I googled and find out about NSURLConnection for GET/POST/DELETE. I can GET data by the below codes,

  NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
 [request setURL:[NSURL URLWithString:@"http://************/api/Users"]];
 [request setHTTPMethod:@"GET"];
 [request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"Content-
 Type"];
 NSURLResponse *response;
 NSData *GETData = [NSURLConnection sendSynchronousRequest:request   
 returningResponse:&response error:nil];
 NSString *ResultData = [[NSString alloc] initWithBytes:[GETData bytes] length:[GETData 
  length] encoding: NSASCIIStringEncoding];
 NSLog(@"ResultData: %@", ResultData);

But for POST method, i doesn't get any ideas of , how it functions and how it store record to sql server database, can't understand whether it s storing data or not, i tried the following codes,

username = @"Aravind.k";
password = @"1234/";
email = @"sivaarwin@gmail.com";

NSString *post = [NSString  stringWithFormat:@"FirstName=%@&LastName=%@&WorkEmailAddress=%@",
    username, password, email];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding 
     allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://*********/api/Users"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded;charset=UTF-8"  
forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSLog(@"request: %@", request);
NSLog(@"postData: %@", postData);
NSURLResponse *response;
NSData *POSTReply = [NSURLConnection sendSynchronousRequest:request 
                                          returningResponse:&response 
                                                      error:nil];    
NSLog(@"POSTReply: %@", POSTReply);    
NSString *theReply = [[NSString alloc] initWithBytes:[POSTReply bytes] 
                                              length:[POSTReply length] 
                                            encoding:NSASCIIStringEncoding];

NSLog(@"Reply: %@", theReply);

Same api url for both GET and POST, any suggestions regarding POST and DELETE will be grateful, Thanks in advance.And how we can get notified as the entered data is stored into the server.


回答1:


First off: not checking for errors automatically leads to down votes ;)

Please edit your code with full error checks!

When using a MIME type application/x-www-form-urlencoded you need to properly encode the parameters.

I would suggest, to create a NSDictionary holding your parameters, e.g.:

NSDictionary* params = @{@"FirstName": firstName, @"LastName": lastName, @"password": password};

and then use the approach described in the following link to get an encoded parameter string suitable for using as a body data for a application/x-www-form-urlencoded message:

How to send multiple parameterts to PHP server in HTTP post

The link above implements a Category and a method dataFormURLEncoded for a NSDictionary which returns an encoded string in a NSData object:

NSData* postData = [params dataFormURLEncoded];

Note: The MIME type application/x-www-form-urlencoded does not have a charset parameter. It will be ignored by the server. You should set the header like below:

[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

Otherwise, your code should work. I would strongly recommend to use the asynchronous version of the convenient method:

+ (void)sendAsynchronousRequest:(NSURLRequest *)request 
                          queue:(NSOperationQueue *)queue 
              completionHandler:(void (^)(NSURLResponse*, NSData*, NSError*))handler


来源:https://stackoverflow.com/questions/20066001/api-post-method-not-saving-data-in-sql-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!