Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I send post and header data with Chromium?

I am attempting to convert some code from TWebBrowser to Chromium but am having trouble figuring out how to send post and header data with an HTTP request.

Below is the TWebBrowser functionality I'm trying to implement.

var
VHeader, PostData: OleVariant;


PostData := VarArrayCreate([0, Length(XMLString) - 1], varByte) ;    
HeaderData := 'Content-Type: application/x-www-form-urlencoded'+ '\n';

WebBrowser1.Navigate(StrUrl,EmptyParam,EmptyParam,PostData,VHeader);

How do I do the equivalent with Chromium?

like image 706
Trevor Avatar asked Oct 19 '12 16:10

Trevor


1 Answers

Due to a missing documentation for Delphi Chromium Embedded, I'll refer the needed requirements for sending web requests for the C++ version of CEF. So, you need to use the LoadRequest method for sending requests in Chromium. For using it, you need the object instance of the CefRequest request object class along with the HeaderMap and CefPostData objects for request header and data specification.

Expanding on Henri Gourvest's (author of the Delphi CEF wrapper) example from this thread, you can in Delphi try something like the following pseudo-code:

uses
  ceflib;

function CreateField(const AValue: AnsiString): ICefPostDataElement;
begin
  Result := TCefPostDataElementRef.New;
  Result.SetToBytes(Length(AValue), PAnsiChar(AValue));
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  Header: ICefStringMultimap;
  Data: ICefPostData;
  Request: ICefRequest;
begin
  Header := TCefStringMultimapOwn.Create;
  Header.Append('Content-Type', 'application/x-www-form-urlencoded');

  Data := TCefPostDataRef.New;
  Data.AddElement(CreateField('Data.id=27'));
  Data.AddElement(CreateField('&Data.title=title'));
  Data.AddElement(CreateField('&Data.body=body'));

  Request := TCefRequestRef.New;
  Request.Flags := WUR_FLAG_NONE;
  Request.Assign('http://example.com/', 'POST', Data, Header);

  Chromium1.Browser.MainFrame.LoadRequest(Request);
end;

The same should do another version of the above code:

procedure TForm1.Button1Click(Sender: TObject);
var
  Header: ICefStringMultimap;
  Data: ICefPostData;
  Request: ICefRequest;
begin
  Request := TCefRequestRef.New;
  Request.Url := 'http://example.com/';
  Request.Method := 'POST';
  Request.Flags := WUR_FLAG_NONE;

  Header := TCefStringMultimapOwn.Create;
  Header.Append('Content-Type', 'application/x-www-form-urlencoded');
  Request.SetHeaderMap(Header);

  Data := TCefPostDataRef.New;
  Data.AddElement(CreateField('Data.id=27'));
  Data.AddElement(CreateField('&Data.title=title'));
  Data.AddElement(CreateField('&Data.body=body'));
  Request.PostData := Data;

  Chromium1.Browser.MainFrame.LoadRequest(Request);
end;
like image 157
TLama Avatar answered Sep 22 '22 20:09

TLama