我正在使用数据存储区开发 Google 云端点。当我使用方法 POST 通过 Android 和 iOS 传递数据时,它会以空记录保存在数据存储中。即请求正常工作,但我发送数据的方式是错误的。请帮我这个。 对于iOS,代码如下:
NSDictionary *headers = @{ @"content-type": @"application/json",
@"cache-control": @"no-cache",
@"postman-token": @"404012ff-722e-c5d8-48db-fa7fb3260841"};
NSLog(@"header %@",headers);
request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"enter url here..."]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10.0];
NSString* strRequest=@"";
strRequest =[[NSString alloc] initWithFormat:@"displayEmail=%@&displayName=%@",@"sonal@gmail.com",@"sonal"];
NSLog(@"url is %@ parameter = %@", request , strRequest);
[request setHTTPMethod:@"POST"];
[request setHTTPBody: [strRequest dataUsingEncoding:NSUTF8StringEncoding]];
[request setAllHTTPHeaderFields:headers];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSString *responseBody = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"responce Body===>%@",responseBody);
if (error) {
NSLog(@"%@", error);
} else {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
NSLog(@"responce%@", httpResponse);
}
}];
[dataTask resume];
最佳答案
您需要设置请求的httpBody
属性。如果您不提供 POST 正文,当然它是空白的。
https://developer.apple.com/reference/foundation/nsmutableurlrequest/1409064-httpbody
或者 Objective-C
https://developer.apple.com/reference/foundation/nsmutableurlrequest/1409064-httpbody?language=objc
关于ios - 通过 iOS 向 Google Cloud 端点发送数据时请求为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40910452/