[英]Not getting JSON response using content-type application/JSON iOS
[英]iOS - Https POST with Forms Still Recognised As JSON Content-Type
我正在使用以下代码使用内容类型,形式的参数执行https POST。
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", URL_IHEARTRADIO_API, URL_USER_LOGIN]]];
[request setHTTPMethod:@"POST"];
NSError *error;
NSString *dataString = @"key1=value1&key2=value2";
NSData *data = [dataString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
[request setValue:[@([dataString length]) stringValue] forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:dataString];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
[connection start];
但是,我总是从服务器收到响应,错误代码为400。此外,即使我将内容类型设置为application / x-www-form-urlencoded,它也指出内容类型为application / json。
这是服务器的响应:
status code: 400, headers {
"Accept-Ranges" = bytes;
"Access-Control-Allow-Headers" = "X-hostName, X-User-Id, X-Session-Id, Content-Type";
"Access-Control-Allow-Origin" = "*";
Connection = "keep-alive";
"Content-Type" = "application/json;charset=UTF-8";
Date = "Thu, 23 Jun 2016 16:48:11 GMT";
Server = "Apache-Coyote/1.1";
"Transfer-Encoding" = Identity;
Vary = "Accept-Encoding, X-hostName, X-Accept";
Via = "1.1 varnish";
"X-Cache" = MISS;
"X-Cache-Hits" = 0;
"X-Served-By" = "cache-sjc3643-SJC";
"X-Timer" = "S1466700491.006144,VS0,VE75";
}
"Content-Type" = "application/json;charset=UTF-8";
是响应内容类型而不是请求。
您应该检查JSON响应,以了解为什么会出现400错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.