Delphi Devart SecureBridge POST Request

Viewed 822

I am using the trial version of DevArt's SecureBridge product. I am trying to process POST, but somehow I could not print the request data.

XML:

<test>
<a>test1</a>
<b>test2</b>
</test>

Delphi:

  ScHttpWebRequest1.Method := rmPOST;
  ScHttpWebRequest1.ContentType := 'text/xml';
  ScHttpWebRequest1.RequestUri := 'https://test.com/api';
  ScHttpWebRequest1.KeepAlive := True;
  ScHttpWebRequest1.ContentLength := Length(XML);
  ScHttpWebRequest1.WriteBuffer(pAnsiChar(XML), 0, Length(XML)); ///I think I'm making a mistake here.
  ShowMessage(ScHttpWebRequest1.GetResponse.ReadAsString);

I have reviewed the documents, but there is a feature called RequestStream. This feature was not available in the version I downloaded. I think WriteBuffer is used instead or different. all I want to do is request a POST with XML content on the relevant site. How can I do it?

Thanks.

2 Answers

Here's a chunk of code that has worked for me:

var
 Response: TScHttpWebResponse;
 ResponseStr: string;
 buf: TBytes;
begin
  ScHttpWebRequest1.Method := rmPOST;
  ScHttpWebRequest1.ContentType := 'text/xml';
  ScHttpWebRequest1.RequestUri := 'https://test.com/api';
  ScHttpWebRequest1.KeepAlive := True;

   buf := TEncoding.UTF8.GetBytes(xml);
   ScHttpWebRequest1.ContentLength := Length(buf);
   ScHttpWebRequest1.WriteBuffer(buf);
   Response:=ScHttpWebRequest1.GetResponse;
   ResponseStr:=Response.ReadAsString;
end;

Based on Devart forums information you can post/put stream or strings parameters as below:

var
  Request: TScHttpWebRequest;
  Response: TScHttpWebResponse;
  ResponseStr: string;
  Stream: TFileStream;
begin
  Request := TScHttpWebRequest.Create(URL);
  Stream := TFileStream.Create(FileName, fmOpenRead);
  try
    Request.Method := rmPut;

    Request.ContentType := 'application/pdf';
    Request.TransferEncoding := 'binary';
    Request.Headers.Add('Content-Disposition', 'form-data; name="FormFile"; filename="Document1.pdf"');

    Request.ContentLength := Stream.Size;
    Request.SendChunked := True;
    Request.RequestStream := Stream;

    Response := Request.GetResponse;
    ResponseStr := Response.ReadAsString;
    Response.Free;
  finally
    Stream.Free;
    Request.Free;
  end;
end;
Related