title: Unable to do streamed request with bounded stream pair
body: <p>I am trying to make the streamed request work by using stream pairs. For test purpose, I prerecorded a sound and use a stream pair and write the contents with the output stream and pass the input stream to the completion handler of this method</p>
  
  <pre><code>func urlSession(_ session: URLSession, task: URLSessionTask, needNewBodyStream completionHandler: @escaping (InputStream?) -&gt; Void)
  </code></pre>
  
  <p>The request is successful with no stream pairs. Just prepare the data first and initialize an Input Stream with it and pass it to the completion handler of the method above. Here's the steps that worked for me</p>
  
  <ol>
  <li>Record sound</li>
  <li>Prepare the request(This time, the needNewBodyStream method will be called)</li>
  <li>Prepare the body data</li>
  <li>Pass in the input stream(initialized from the body data)to the completion handler</li>
  </ol>
  
  <p>Code:</p>
  
  <pre><code>func urlSession(_ session: URLSession, task: URLSessionTask, needNewBodyStream completionHandler: @escaping (InputStream?) -&gt; Void)
  {
      NSLog("need new body stream")
  
      var bodyData = Data()
      bodyData.append(getBoundaryBeginData())
  
      bodyData.append(getJsonMessageHeaderData())
      bodyData.append(AVSRecognizeRequest().jsonData())
      bodyData.append("\r\n".data(using: String.Encoding.utf8)!)
  
      bodyData.append(getBoundaryBeginData())
      bodyData.append("Content-Disposition: form-data; name=\"audio\"\r\n".data(using: String.Encoding.utf8)!)
      bodyData.append("Content-Type: application/octet-stream\r\n\r\n".data(using: String.Encoding.utf8)!)
  
      try! bodyData.append(AudioManager.shared.getRecording()!)
  
      bodyData.append("\r\n".data(using: String.Encoding.utf8)!)
      bodyData.append(getBoundaryEndData())
  
      NSLog("Total data length is \(bodyData.count)")
  
      completionHandler(InputStream.init(data: bodyData))
  }
  </code></pre>
  
  <p>However, if I pass the input stream (from the stream pair) to the completion handler then write the contents after that, the request fails with a 400 error. The ff. are the steps that fails:</p>
  
  <ol>
  <li>Record sound</li>
  <li>Prepare the body data</li>
  <li>Get stream pair</li>
  <li>Prepare the request(This time, the needNewBodyStream method will be called)</li>
  <li>Pass in the input stream to the completion handler</li>
  <li>Write data with output stream</li>
  </ol>
  
  <p>When I check the delegate method <code>func urlSession(_ session: URLSession, task: URLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64)</code> the total bytes sent was only 8192. But my total data length was about 80k.</p>
  
  <p>Code:</p>
  
  <pre><code>public func stopRecognizing()
  {
      //*** 1. RECORD A SOUND  
  
      AudioManager.shared.stopRecording()
  
      //*** 2. PREPARE BODY DATA
  
      var bodyData = Data()
      bodyData.append(getBoundaryBeginData())
  
      bodyData.append(getJsonMessageHeaderData())
      bodyData.append(AVSRecognizeRequest().jsonData())
      bodyData.append("\r\n".data(using: String.Encoding.utf8)!)
  
      bodyData.append(getBoundaryBeginData())
      bodyData.append("Content-Disposition: form-data; name=\"audio\"\r\n".data(using: String.Encoding.utf8)!)
      bodyData.append("Content-Type: application/octet-stream\r\n\r\n".data(using: String.Encoding.utf8)!)
  
      try! bodyData.append(AudioManager.shared.getRecording()!)
  
      bodyData.append("\r\n".data(using: String.Encoding.utf8)!)
      bodyData.append(getBoundaryEndData())
  
      //*** 3. GET STREAM PAIR
  
      Stream.getBoundStreams(withBufferSize: bodyData.count, inputStream: &amp;inputStream, outputStream: &amp;outputStream)
  
      //*** 4. PREPARES THE REQUEST (AFTER THIS IS CALLED, the needNewBodyStream delegate method will be called)
      sendRecognizeRequest()
  
      //*** 6. WRITE THE DATA WITH OUTPUT STREAM
  
      outputStream?.open()
  
      bodyData.withUnsafeBytes(
      {(bytes: UnsafePointer&lt;UInt8&gt;) -&gt; Void in
  
          outputStream?.write(bytes, maxLength: bodyData.count)
  
      })
  }
  
  
  func urlSession(_ session: URLSession, task: URLSessionTask, needNewBodyStream completionHandler: @escaping (InputStream?) -&gt; Void)
  {
      NSLog("need new body stream")
  
      //*** 5. PASS IN THE INPUT STREAM
  
      completionHandler(inputStream)
  }
  </code></pre>
  
  <p>I tested a stream pair my self by running a thread that continuously reads from the input stream and on the other hand, an output stream that writes data continuously. I can clearly that the data written to other stream can be read from the input stream given that they are already bound.</p>
  
  <p>So, why didn't my streamed request work? </p>