gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 17:49:18 25 4
gpt4 key购买 nike

当我发布像

这样的简单 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com