1

I can't seem to be able to completely remove headers from a request. I tried subclassing a NSURLRequest and setting header values to nil, but they still show up. The closest thing I was able to do is this:

-(instancetype)init{
self = [super initWithURL:[NSURL URLWithString:@"http://www.example.com/"]];
if(self){
    self.HTTPMethod = @"POST";
    self.HTTPBody = [@"65000,100" dataUsingEncoding:NSUTF8StringEncoding];
    [self setValue:@"" forHTTPHeaderField:@"Host"];
    [self setValue:@"" forHTTPHeaderField:@"User-Agent"];
    [self setValue:@"" forHTTPHeaderField:@"Content-Type"];
    [self setValue:@"" forHTTPHeaderField:@"Connection"];
    [self setValue:@"" forHTTPHeaderField:@"Accept"];
    [self setValue:@"" forHTTPHeaderField:@"Accept-Language"];
    [self setValue:@"" forHTTPHeaderField:@"Accept-Encoding"];
    [self setValue:@"" forHTTPHeaderField:@"Content-Length"];
}
return self;
}

This creates these headers:

POST / HTTP/1.1
Host: 
Content-Type: 
Content-Length: 9
Accept: 
User-Agent: 
Accept-Language: 
Accept-Encoding: 
Connection: keep-alive

65000,100

And ideally I'm trying to just send on the request

65000,100

Maybe using a NSURLSessionTask with a NSURLRequest is not the way to do this, so if that is the case, please advise.

Jan
  • 2,142
  • 2
  • 27
  • 50
  • I don't think you will be able to do this with NSURLSession. NSURLSession adds these headers automatically on the lower level. Check [this thread](https://stackoverflow.com/questions/39627616/preventing-nsurlsession-default-http-headers) maybe you will find some useful information there. You can also give a CFNetwork a try. It's a lower level API and it may give you some more control over the entire process. – Kamil Szostakowski Oct 20 '17 at 21:09

1 Answers1

0

I don't think you want to do this across the board, since it violates the HTTP spec in many cases. For example, the Host header MUST be included in HTTP/1.1 [14.23]. Same with Connection. Content-Length, User-Agent SHOULD be included.

However, I believe that the old method of clearing some of these fields by setting them to nil no longer works in iOS10+.

greymouser
  • 2,774
  • 18
  • 22
  • I don't intend to do this across the board, but I'm working on an implementation where we have total control of the server and every single byte counts. Adding all of that to the headers is a LOT of overhead in our scenario. – Jan Oct 20 '17 at 17:20