title: Delphi TWebBrowser PostData incomplete for OnBeforeNavigate2 event
body: <p>I want to modify the headers of a request with TWebBrowser in Delphi XE5.  As described in <a href="https://stackoverflow.com/questions/9210507/modify-requestheaders-in-custom-browser-in-delphi">Modify requestHeaders in “custom” browser in delphi</a>, you can do that using the OnBeforeNavigate2 event of the TWebBrowser. The procedure corresponding to the event would be:</p>
  
  <pre><code>procedure TForm1.WebBrowser1BeforeNavigate2(Sender: TObject;
    const pDisp: IDispatch; var URL, Flags, TargetFrameName, PostData,
    Headers: OleVariant; var Cancel: WordBool);
  
  var
    NewHeaders: OleVariant;
  begin
    // some code to stop the navigation, enhance the headers and avoid re-enterng the procedure
  
    // do not allow frames or iframes to raise this event
    if (pDisp as IUnknown) = (WebBrowser1.ControlInterface as IUnknown)     then
    begin
      // avoid stack overflow: check if our custom header is already set
      if Pos('MyHeader', Headers) &lt;&gt; 0 then Exit;
  
      // cancel the current navigation
      Cancel := True;
      (pDisp as IWebBrowser2).Stop;
  
      // modify headers with our custom header
      NewHeaders := Headers + 'MyHeader: Value'#13#10;
      // now proceed with navigation 
      (pDisp as IWebBrowser2).Navigate(URL, Flags, TargetFrameName, PostData, NewHeaders);
    end;
  end;
  </code></pre>
  
  <p>The problem is that while the method proposed in the answer works for a GET request, for a POST request I run into the following problem: PostData only contains the beginning of the initially posted data (in my case a text file uploaded via a html form). Concretely, while a correct example of what a client sends back to the server would look like this:</p>
  
  <pre><code>--AaB03x
      content-disposition: form-data; name="field1"
  
      Joe Blow
      --AaB03x
      content-disposition: form-data; name="pics"; filename="file1.txt"
      Content-Type: text/plain
  
       ... contents of file1.txt ...
  --AaB03x--
  </code></pre>
  
  <p>what I find while debugging as PostData is something like this
  PostString </p>
  
  <pre><code>'-----------------------------7e16'#$D#$A'Content-Disposition: form-data; name="myform"; filename="MyUploadedFile.csv"'#$D#$A'Content-Type: application/vnd.ms-excel'#$D#$A#$D#$A#0
  </code></pre>
  
  <p>Hence the entire content of the file is missing and the final boundary (i.e. -----------------------------7e16). </p>
  
  <p>WireShark tells me that despite PostData is not NULL <code>Navigate()</code> is not making a POST request but a GET with a message body being exactly what I found during debugging and show above. </p>
  
  <p>My guess is that the reason for the latter is that the <code>Navigate()</code> function performs a check on whether PostData is valid before it creates a POST request and it makes a GET otherwise.</p>
  
  <p>Since someone claims that <code>Navigate()</code> works correctly with PostData what can I do to get the complete PostData for my <code>OnBeforeNavigate2</code> event?</p>