我正在使用数据存储来开发Google Cloud端点。当我使用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

10-02 10:49
查看更多