Adding Parameters to TscHTTPWebRequest (Delphi)

Discussion of open issues, suggestions and bugs regarding network security and data protection solution - SecureBridge
Post Reply
BlurryDog
Posts: 2
Joined: Mon 07 Mar 2022 17:03

Adding Parameters to TscHTTPWebRequest (Delphi)

Post by BlurryDog » Mon 07 Mar 2022 23:22

I need to add some parameters for a POST call.
How do I go about doing this using TscHTTPWebRequest?
Is it as simple as just adding a header?
With other components (TRestCLient, Indy..) I used AddParameter(Name, value).
Do I just use a WriteBuffer or use the RequestStream property?

Can someone show me how to add these parameters ?

grant_type:password
username:SomeUsername==
password:SomePassword==
scope:openid ability:accessapi

TheUnknow
Posts: 2
Joined: Wed 22 Aug 2018 00:46

Re: Adding Parameters to TscHTTPWebRequest (Delphi)

Post by TheUnknow » Tue 08 Mar 2022 05:10

Basically, you can add your parms to a string and then convert them to bytes. Once done WrtieBuffer then GetResonse can be use. I not sure which format your params are supposed to be in so I just wrote it out as "application/x-www-form-urlencoded" post.

var
buf : tbytes;
Str : string;
WebClient : TScHTTPWebRequest;
begin

WebClient := TScHTTPWebRequest.create;
try
WebClient.Method := TScRequestMethod.rmPOST;
WebClient.ContentType := 'application/x-www-form-urlencoded'; //you would of course put which contentType you using.
WebClient.RequestUri := 'http://somesite.tld';

Str := 'grant_type=' + passwordVar + '&username=' + usernameVar '&password=' + passwordVar + '&scope=' + OpenIdVar + '&ability-' + accessApiVar;

buf := TEncoding.UTF8.GetBytes(Str);
WebClient.WriteBuffer(buf);
WebClient.GetResponse; //returns TScHttpWebResponse or could get string by using GetResponse.ReadAsString
finally
WebClient.free;
end;

end;

Raudar
Devart Team
Posts: 19
Joined: Fri 02 Apr 2021 11:04

Re: Adding Parameters to TscHTTPWebRequest (Delphi)

Post by Raudar » Tue 22 Mar 2022 15:42

Hi there,

TheUnknow- thanks a lot for your comment, you described the correct approach of REST requests.

Best Regards,

Devart Support

Post Reply