title: Status code 400: NSHTTPURLResponse on calls made using URLSession.shared.dataTask
body: <p>This is an issue that I started having after I updated my iOS 11 (15A5304i) and Xcode 9 (9M137d) to the latest beta. </p>
  
  <p>When I revert back to the previous beta, it goes away. </p>
  
  <p>Making any GET https calls to a website results in a 400 response from the servers. </p>
  
  <pre><code>networkError(FoodAPI.NetworkError.responseStatusError(status: 400, message: "&lt;NSHTTPURLResponse: 0x1c4239700&gt; { URL: https://stackoverflow.com/ } { status code: 400, headers {\n    Date = \"Thu, 22 Jun 2017 17:41:03 GMT\";\n} }"))
  </code></pre>
  
  <p>This is how I'm making the call </p>
  
  <pre><code>func callTask(_ request: URLRequest, completion: @escaping (ResultType&lt;Data, NetworkError&gt;) -&gt; Void) {
    let task = URLSession.shared.dataTask(with: request) { data, response, error in
  
      guard let response = response as? HTTPURLResponse else {
        completion(.failure(.responseInvalidFormat))
        return
      }
  
      if response.statusCode &gt;= 400 {
        let error = response.description
        completion(.failure(.responseStatusError(status: response.statusCode, message: error)))
        return
      }
  
      guard let data = data else {
        completion(.failure(.responseNoDataError))
        return
      }
  
      completion(.success(data))
    }
    task.resume()
  }
  </code></pre>
  
  <p>and this is how the request is generated:</p>
  
  <pre><code>  private func generateRequest(urlString: String,
                                 method: HttpMethod = .get,
                                 contentType: ContentType = .json,
                                 headers: [String: String] = ["":""],
                                 body: [String: Any]? = nil) throws -&gt; URLRequest {
  
  
      guard let url = URL(string: urlString) else {
        throw NetworkError.requestGenerationFail(url: urlString, httpMethod: method)
      }
      var request = URLRequest(url: url)
      request.httpMethod = method.rawValue
      request.setValue("***** by Reza: info: reza@example.com", forHTTPHeaderField: "User-Agent")
  
      request.allHTTPHeaderFields = headers
      if contentType == .urlEncoded &amp;&amp; headers["content-type"] ?? "" != ContentType.urlEncoded.rawValue {
        request.addValue(ContentType.urlEncoded.rawValue, forHTTPHeaderField: "content-type")
      }
  
      if let body = body {
        request.httpBody = try self.generateBody(body: body, contentType: contentType)
      }
  
      return request
    }
  </code></pre>
  
  <p>Again, this works fine on the previous iOS 11 beta, however the new one results in a 400 response on https calls to web servers, with little to no data.</p>
  
  <p>It's worth noting that I first make a call to Yelp's endpoint API which is hosted on <a href="https://api.yelp.com/" rel="nofollow noreferrer">https://api.yelp.com/</a>. This connection succeeded without any issue. Any further calls to other host (<a href="https://stackoverflow.com">https://stackoverflow.com</a>, <a href="https://www.yelp.com" rel="nofollow noreferrer">https://www.yelp.com</a>) comes back with a 400. </p>
  
  <p>When I run the code in simulator using the old beta, it works fine, when I deploy to my iPhone 6 that uses the new iOS it results in 400 from servers. </p>
  
  <p>I haven't tested this against a server I own so I can view logs, but something along the way is breaking and causing servers to comeback with a bad request response. These same calls work fine using curl, postman, browsers and previous iOS builds.</p>
  
  <p>I was hoping someone who has faced the same issue could shed some light.</p>