I am attempting to POST some simple JSON to a RESTful service. I want to form a json string that takes the following form:
{username:"someuser",password:"somepassword"}
My POST keeps failing at the server complaining that POSTed values do not exist. However the same service works fine if I create a JS test implementation. So I'm pretty sure its my SBJson client implementation that is the problem.
NSDictionary *userDict = [[NSDictionary alloc] initWithObjectsAndKeys:
userName,@"username",
password, @"password",
nil];
SBJsonWriter *writer = [[SBJsonWriter alloc]init];
NSData *jsonData = [writer dataWithObject:userDict];
jsonData appears to have size but it attempt to serialize it with SBJson its fails and I get nil.
NSString *test = [writer stringWithObject:jsonData]; // returns nil
I am trying to post the above in a NSMutableURLRequest. Here's my implementation if it helps give context:
-(void)registerUserName:(NSString *)userName andPassword:(NSString *)password{
responseData = nil;
responseData = [NSMutableData data];
NSDictionary *userDict = [[NSDictionary alloc] initWithObjectsAndKeys:
userName,@"username",
password, @"password",
nil];
SBJsonWriter *writer = [[SBJsonWriter alloc]init];
NSData *jsonData = [writer dataWithObject:userDict];
NSString *test = [writer stringWithObject:jsonData]; //always nil
NSURL *url = [[NSURL alloc] initWithString:@"http://localhost/~user/app_web/index.php/user/create"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:@"POST"];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request addValue:@"form-data" forHTTPHeaderField:@"Content-Disposition"];
[request setHTTPBody:jsonData];
[[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:YES ];
currentRequestType = REGISTER_USER;
NSURLConnection *connection =[[NSURLConnection alloc]initWithRequest:request delegate:self];
Thanks!
update
Using the suggestions below I am now doing this:
NSDictionary *userDict = [[NSDictionary alloc] initWithObjectsAndKeys:
userName,@"username",
password, @"password",
nil];
NSString *jsonString = [userDict JSONRepresentation];
NSData *jsonData = [jsonString dataUsingEncoding];
[request setHTTPBody:jsonData];
[[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:YES ];
currentRequestType = REGISTER_USER;
NSURLConnection *connection =[[NSURLConnection alloc]initWithRequest:request delegate:self];
Unfortunately my posted values are still not found at the server. I think something must be wrong with my NSMutableURLRequest..
I am using CodeIgniter on the backend. The action looks like this:
public function create(){
if($this->input->post('username') && $this->input->post('password')){
$username = $this->input->post('username');
$password = $this->input->post('password');
$this->load->model('User_model','users');
$status = $this->users->add_user($username,$password);
if($status){
echo json_encode(array('success'=>true));
} else{
echo json_encode(array('success'=>false));
}
}
echo json_encode(array('success'=>false)); // when posting I arrive here
}
To serialize the dictionary userDict
call the following method.
NSString* jsonString = [userDict JSONRepresentation];
If you want the NSData representation you can use the following method
NSData* jsonData = [jsonString dataUsingEncoding:NSUTF8Encoding];
try this:
NSDictionary *userDict = [[NSDictionary alloc] initWithObjectsAndKeys:
userName,@"username",
password, @"password",
nil];
SBJsonWriter *writer = [[SBJsonWriter alloc]init];
NSString *test = [writer stringWithObject:userDict];
don't forget to release them
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.