I managed to make an application to send data to a rest api service. The content type must be text/html. But whenever i run my app i get a 415 http code response which means not supported type of content. Here is my code:
NSURL *url = [NSURL new];
NSData *body = nil;
NSString *contentType = @"text/html";
NSURL *finalURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://telesto.zapto.org:81/SMART_EdgeNode/EdgeNode/DataFeeds/3/addMeasurement"]];
NSString *yourString = @"geoX#35#geoY#65";
contentType = @"application/x-www-form-urlencoded; charset=utf-8";
body = [[NSString stringWithFormat:@"%@", yourString] dataUsingEncoding:NSUTF8StringEncoding];
NSString *putLength = [NSString stringWithFormat:@"%d",[body length]];
if (nil==finalURL) {
finalURL = url;
}
NSMutableDictionary* headers = [[[NSMutableDictionary alloc] init] autorelease];
[headers setValue:contentType forKey:@"Content-Type"];
[headers setValue:@"mimeType" forKey:@"Accept"];
[headers setValue:@"no-cache" forKey:@"Cache-Control"];
[headers setValue:@"no-cache" forKey:@"Pragma"];
[headers setValue:@"close" forKey:@"Connection"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:finalURL
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[request setHTTPMethod:@"PUT"];
[request setAllHTTPHeaderFields:headers];
[request setHTTPBody:body];
[request setValue:putLength forHTTPHeaderField:@"Content-Length"];
self.conn = [NSURLConnection connectionWithRequest:request delegate:self];
I guess atm there is something wrong with defining the content-type . Any help??
Thanks in advance
Ok i found it at last. It was this specific line that was causing the problem and had to do something with the content-type :
contentType = @"application/x-www-form-urlencoded; charset=utf-8";
After removing that i was able to send properly my string to the server.
I think the server's complaining that it doesn't know how to generate a response with the content type "mimeType".
Your Accept header should be "text/html" or whatever the content type you expect in the response, not "mimeType".
Update
After reading this again, I notice that you are setting the contentType to @"text/html"
, then to @"application/x-www-form-urlencoded; charset=utf-8"
. Which do you need? From the description, you want to sent text/html
, but that is not what your sending. When you set contentType
to @"application/x-www-form-urlencoded; charset=utf-8"
, text/html
is lost.
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.