NSData 返回 0 字节
NSData is returning 0 bytes
我正在尝试执行一个简单的 POST 请求。但是,POSTMAN 插件在 chrome 和 iOS 模拟器中的结果似乎不同。
这是来自 POSTMAN 的快照:
如你所见,我在 retun 中得到了 JSON 数据。
这是我执行 POST 请求的代码:
NSError *error;
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
NSURL *url = [NSURL URLWithString:kPostURL];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPMethod:@"POST"];
NSString *params =[[NSString alloc] initWithFormat:@"fname=%@&lname=%@&email=%@&password=%@&switchid=%d&didflag=%@",fname,lname,email,pass,switchid,flag];
[request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(@"response is %@",response);
NSLog(@"erros is %@",error);
NSMutableDictionary * innerJson = [NSJSONSerialization
JSONObjectWithData:data options:kNilOptions error:&error
];
NSLog(@"JSON data is %@",innerJson);
}];
[postDataTask resume];
当我尝试在调试器中打印值时,我得到
JSON
为 null
,NSData
为 0
bytes
。但是我得到了 status code as 200
这是成功的。
这是我得到的回复:
{ status code: 200, headers {
Connection = "Keep-Alive";
"Content-Length" = 0;
"Content-Type" = "text/html";
Date = "Thu, 25 Feb 2016 02:04:02 GMT";
"Keep-Alive" = "timeout=5";
Server = "Apache/2.4.12";
"X-Powered-By" = "PHP/5.5.30";
} }
为什么我得到的 NSData 为 0 字节?
在 chrome 中的请求中,您的参数为 URL 参数。在 ObjC 版本中,您将参数添加为 post.
的一部分
而不是将参数添加为正文的一部分:
[request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
将其添加为查询的一部分:
NSURL *url = [NSURL URLWithString:[kPostURL stringByAppendingFormat:@"?%@", params]];
我正在尝试执行一个简单的 POST 请求。但是,POSTMAN 插件在 chrome 和 iOS 模拟器中的结果似乎不同。
这是来自 POSTMAN 的快照:
如你所见,我在 retun 中得到了 JSON 数据。
这是我执行 POST 请求的代码:
NSError *error;
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
NSURL *url = [NSURL URLWithString:kPostURL];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPMethod:@"POST"];
NSString *params =[[NSString alloc] initWithFormat:@"fname=%@&lname=%@&email=%@&password=%@&switchid=%d&didflag=%@",fname,lname,email,pass,switchid,flag];
[request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(@"response is %@",response);
NSLog(@"erros is %@",error);
NSMutableDictionary * innerJson = [NSJSONSerialization
JSONObjectWithData:data options:kNilOptions error:&error
];
NSLog(@"JSON data is %@",innerJson);
}];
[postDataTask resume];
当我尝试在调试器中打印值时,我得到
JSON
为 null
,NSData
为 0
bytes
。但是我得到了 status code as 200
这是成功的。
这是我得到的回复:
{ status code: 200, headers {
Connection = "Keep-Alive";
"Content-Length" = 0;
"Content-Type" = "text/html";
Date = "Thu, 25 Feb 2016 02:04:02 GMT";
"Keep-Alive" = "timeout=5";
Server = "Apache/2.4.12";
"X-Powered-By" = "PHP/5.5.30";
} }
为什么我得到的 NSData 为 0 字节?
在 chrome 中的请求中,您的参数为 URL 参数。在 ObjC 版本中,您将参数添加为 post.
的一部分而不是将参数添加为正文的一部分:
[request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
将其添加为查询的一部分:
NSURL *url = [NSURL URLWithString:[kPostURL stringByAppendingFormat:@"?%@", params]];