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
Adding Parameters to TscHTTPWebRequest (Delphi)
Re: Adding Parameters to TscHTTPWebRequest (Delphi)
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;
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;
Re: Adding Parameters to TscHTTPWebRequest (Delphi)
Hi there,
TheUnknow- thanks a lot for your comment, you described the correct approach of REST requests.
Best Regards,
Devart Support
TheUnknow- thanks a lot for your comment, you described the correct approach of REST requests.
Best Regards,
Devart Support