Web Input Extraction Conventions Perl

• As part of a POST request , such as a form subm ission t hat specifies method=POST in t he form t ag. The cont ent s of a form for a POST request are sent as input param et ers in t he body of t he request , rat her t han at t he end of t he URL. You m ay also have occasion t o process ot her t ypes of input , such as uploaded files. Those are sent using POST request s, but as part of a special kind of form t hat is discussed in Recipe 18.9 . When you gat her input for a web script , you m ay need t o be concerned wit h how t he input was sent . Som e API s dist inguish bet ween input sent via GET and POST , ot her s do not . However, once you have pulled out t he inform at ion t hat was sent , t he request m et hod doesnt m at t er. The validat ion and query const ruct ion st ages do not need t o know whet her param et ers were sent using GET or POST . The recipes dist ribut ion includes som e script s in t he apache param s direct ory t om cat m cb for JSP t hat process input param et ers. Each script allows you t o subm it GET or POST request s, and shows how t o ext ract and display t he param et er values t hus subm it t ed. Exam ine t hese script s t o see how t he param et er ext ract ion m et hods for t he various API s are used. Ut ilit y rout ines invoked by t he script s can be found in t he library m odules in t he lib direct ory of t he dist ribut ion.

18.6.4 Web Input Extraction Conventions

To obt ain input param et ers passed t o a script , you should fam iliarize yourself wit h your API s convent ions so t hat you know what it does for you, and what you m ust do yourself. For exam ple, you should know t he answers t o t hese quest ions: • How do you det erm ine w hich param et ers are available? • How do you pull a param et er value from t he environm ent ? • Are values t hus obt ained t he act ual values subm it t ed by t he client , or do you need t o decode t hem furt her? • How are m ult iple- valued param et ers handled for exam ple, if several it em s in a checkbox group are select ed ? • For param et ers subm it t ed in a URL, which separat or charact er does t he API expect bet ween param et ers? This m ay be for som e API s and ; for ot hers. ; is preferable as a param et er separat or because it s not special in HTML like is, but m any browsers or ot her user agent s separat e param et ers using . I f you const ruct a URL wit hin a script t hat includes param et ers at t he end, be sure t o use a param et er separat or charact er t hat t he receiving script will under st and.

18.6.5 Perl

The Perl CGI .pm m odule m akes input param et ers available t o script s t hrough t he param funct ion. param provides access t o input subm it t ed via eit her GET or POST , w hich sim plifies your t ask as t he script writ er. I f a form cont aining id and name param et ers w as subm it t ed via POST , you can process it t he sam e way as if t he param et ers were subm it t ed at t he end of t he URL via GET . You dont need t o perform any decoding, eit her; param handles t hat as well. To obt ain a list of nam es of all available param et ers, call param wit h no argum ent s: names = param ; To obt ain t he value of a specific param et er, pass it s nam e t o param . id = param id; options = param options; I n scalar cont ext , param ret urns t he param et er value if it is single- valued, t he first value if it is m ult iple- values, or undef if t he param et er is not available. I n array cont ext , param ret urns a list cont aining all t he param et ers values, or an em pt y list if t he param et er is not available. A param et er wit h a given nam e m ight not be available if t he form field wit h t he sam e nam e was left blank, or if t here isnt any field wit h t hat nam e. Not e t oo t hat a param et er value m ay be defined but em pt y. For good m easure, you m ay want t o check bot h possibilit ies. For exam ple, t o check for an age param et er and assign a default value of unknown if t he param et er is m issing or em pt y, you can do t his: age = param age; age = unknown if defined age || age eq ; CGI .pm underst ands bot h ; and as URL param et er separat or charact ers.

18.6.6 PHP