当POST数据具有多行时,Google Cloud Endpoints会出错

时间:2015-09-11 06:03:45

标签: objective-c cocoa google-app-engine google-cloud-endpoints

当我发布像

这样的简单JSON时
  

{\" body \":\" test \"," subject \":\" test \&#34 ;}

它工作正常,但当我发布数据时(如下)

  

{" body":" hello

     

名字Paul A Alabisi姓氏客户ID 31877 Serail Key   2512948ead29a82d"," subject":" hi"}

这是错误

  

{"错误":{"错误":[{       "域":"全球",       "原因":" parseError",       "消息":"解析错误" },"代码":400,"消息":"解析错误" }}

这是我的Objective C代码:

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:URL]];

[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

//create the Method "GET" or "POST"
[request setHTTPMethod:@"POST"];

//Pass The String to server
NSString *dataPost = @"{\"body\": \"email body\",\"subject\": \"email subject\"}";

//Check The Value what we passed
NSLog(@"the data Details is =%@", dataPost);

//Convert the String to Data
NSData *data1 = [dataPost dataUsingEncoding:NSUTF8StringEncoding];

//Apply the data to the body
[request setHTTPBody:data1];

//Create the response and Error
NSError *err;
NSURLResponse *response;

NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];

NSString *resSrt = [[NSString alloc]initWithData:responseData encoding:NSASCIIStringEncoding];

//This is for Response
NSLog(@"got response==%@", resSrt);
if(resSrt)
{
    NSLog(@"got response");
    /* ViewController *view =[[ViewController alloc]initWithNibName:@"ViewController" bundle:NULL];
     [self presentViewController:view animated:YES completion:nil];*/
}
else
{
    NSLog(@"faield to connect");
}

1 个答案:

答案 0 :(得分:1)

这是无效的JSON。您必须转义换行符,因为它们是控制字符,例如使用\n代替实际的换行符。

请参阅JSON spec部分2.5。