gpt4 book ai didi

objective-c - 无法从 Cocoa 应用程序 (Objective-C) 将数据发布到 Google 端点。怎么做?

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

我在 Google Endpoints (Python) 的帮助下创建了一个电子邮件实用程序。问题是我无法将数据发布到它。我收到错误。

{ "error": { "errors": [ { "domain": "global", "reason": "parseError", "message": "This API does not support parsing form-encoded input." } ], "code": 400, "message": "This API does not support parsing form-encoded input." } }

下面给出了我在 Objective-C 中所做的事情

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


//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");
}

最佳答案

请参阅:How to send json data in the Http request using NSURLRequest

基本上,您需要设置“Content-Type” header ,以便接收端点不会认为它是表单编码的(默认),而是将其识别为 JSON。

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

关于objective-c - 无法从 Cocoa 应用程序 (Objective-C) 将数据发布到 Google 端点。怎么做?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32507733/

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