我正在使用数据存储区在Google云端点上工作。当我通过Android&使用方法POST的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];
答案 0 :(得分:1)
您需要设置请求的httpBody
属性。如果您没有为POST提供正文,当然则为空白。
https://developer.apple.com/reference/foundation/nsmutableurlrequest/1409064-httpbody
或Objective-C
https://developer.apple.com/reference/foundation/nsmutableurlrequest/1409064-httpbody?language=objc
答案 1 :(得分:0)
我们有一个解决方案.. 我们改变了传递数据的方式.. 这是代码:
request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:syncURL]
cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
NSData *requestData = [NSData dataWithBytes:[serverSyncAPIRequestJSON UTF8String] length:[serverSyncAPIRequestJSON length]];[request setHTTPMethod:@"POST"]; [request setHTTPBody:requestData]; [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];