0

My client send to "abc+def" by Post and get the string again from server.
but return string is "abc def" . why? how do I keep abc+def?

-iOS Client-

NSError * error=nil;
NSString * str=[NSString stringWithFormat:@"selected_card=@"abc+def"];

NSData * postData= [str dataUsingEncoding:NSUTF8StringEncoding];
NSString *myString = [[NSString alloc] initWithData:postData encoding:NSUTF8StringEncoding];
NSLog(@"myString=%@",myString);
[req setHTTPMethod:@"POST"];
[req setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[req setHTTPBody:postData];

NSHTTPURLResponse * response=nil;


NSData *data=[NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error];
if(data!=nil)
{
    NSLog(@"error:%@",error);
    NSDictionary * cardDictionary= [NSJSONSerialization JSONObjectWithData:data
                                                                   options:NSJSONReadingMutableLeaves
                                                                     error:&error];
    NSLog(@"return string:%@",[cardDictionary objectForKey:@"card1"]);
}

-Server

...
$user_table = $_POST['id']."_card";
$select_card_drawing_url=$_POST['selected_card'];
$response=array("card1" => $select_card_drawing_url);
echo json_encode($response);

return ;
?>

-Client Log-

MyString=abc+def
return String: abc def
석진영
  • 243
  • 4
  • 13

1 Answers1

0

You post data in application/x-www-form-urlencoded format. In this format plus sign (+) has special mining: it encodes white space. To get expected result you must encode + as %2b:

NSString * str=[NSString stringWithFormat:@"selected_card=@"abc%2bdef"];

You must encode every parameter value passed in this format (it follows the same encoding rules as parameters in URL). More info about URL encoding in iOS: How do I URL encode a string

Community
  • 1
  • 1
Rimas
  • 5,904
  • 2
  • 26
  • 38