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

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

当我发布像

这样的简单 JSON 时

{\"body\": \"test\",\"subject\": \"test\"}

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

{"body": "hello

First Name Paul A Alabisi Last Name Customer ID 31877 Serail Key 2512948ead29a82d","subject": "hi"}

这是错误

{ "error": { "errors": [ { "domain": "global", "reason": "parseError", "message": "Parse Error" } ], "code": 400, "message": "Parse Error" } }

这是我的 Objective C 代码:

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

[请求 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");
}

最佳答案

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

请参阅JSON spec第 2.5 节。

关于objective-c - 当 POST 数据具有多行时,Google Cloud Endpoints 会出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32516555/

相关文章:

cocoa - 创建快速查看风格的缩放效果

objective-c - 识别哪个关键用户在文本字段中输入

python - Google App Engine 远程 API + OAuth

objective-c - 将 CGEvent 发布到 PSN 时的奇怪行为

ios - 从容器 View 获取数据

ios - 通过FacebookSDK.framework登录时未获取电子邮件ID

objective-c - iOS 应用仅在不调试时崩溃

google-app-engine - 如何在 Google App Engine 的开发服务器上使用拉队列

java - GAE : Request needs more than 30s

objective-c - VoiceOver:在屏幕转换后强制选择辅助功能元素