I am using the following code to perform an https POST with parameters of content type, form.
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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.