简体   繁体   English

iOS-Https POST的形式仍被识别为JSON Content-Type

[英]iOS - Https POST with Forms Still Recognised As JSON Content-Type

I am using the following code to perform an https POST with parameters of content type, form. 我正在使用以下代码使用内容类型,形式的参数执行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];

However, I always get the response from server with error code 400. Plus, it also states that the content-type is application/json even if I am setting it as application/x-www-form-urlencoded. 但是,我总是从服务器收到响应,错误代码为400。此外,即使我将内容类型设置为application / x-www-form-urlencoded,它也指出内容类型为application / json。

This is the response from server: 这是服务器的响应:

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"; is the response content-type not the request. 是响应内容类型而不是请求。

You should check the JSON response as to why you get a 400 error. 您应该检查JSON响应,以了解为什么会出现400错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM