1

How can I get a QueryString Parameter Variable from the URL using Delphi code?

Let's assume I have a URL as follows: https://www.example.com/index.html?first_name=shaun&last_name=roselt

How do I get first_name and last_name from the URL?

I want to do this via Delphi only code and not JavaScript.

1 Answer 1

1

There's a function called GetQueryParam within the WebLib.WebTools unit that comes with TMS Web Core.

You can use it as follows to get the first_name and last_name parameters:

uses WebLib.WebTools;

...

var
  FirstName, LastName: String;
begin
  FirstName := GetQueryParam('first_name');
  LastName := GetQueryParam('last_name');

  if (FirstName = '') then
    console.log('First Name Not Found!')
  else
    console.log('First Name is ' + FirstName);

  if (LastName = '') then
    console.log('Last Name Not Found!')
  else
    console.log('Last Name is ' + LastName);
end;
3
  • How does it behave on edgy (but legal) cases like https://site.web/?one=1&one=2&one=3?
    – AmigoJack
    Sep 27 at 8:39
  • @AmigoJack It will return the last param which is "3" in your case. Sep 27 at 8:45
  • 1
    upvote for question and answer, just what I was looking for, thanks Shaun
    – Alec
    Oct 12 at 17:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.