HTTPGET$()

From Liberty BASIC Family
Jump to navigation Jump to search
Not supported in Just BASIC Not supported in Liberty BASIC Not supported in Liberty BASIC 5 Supported in Run BASIC

Description

The httpget$() function uses the HTTP protocol to get the contents of a web document using a URL. The document is returned as a string.

Syntax

  1. HTTPGET$(urlExpr$) - Perform an HTTP GET and return the result

Hints

Place useful hints about this keyword here

Example

'httpget$() function used to obtain your current external IP address. 
'No parsing is required, the page returns nothing but the IP. 

url$ = "http://www.networksecuritytoolkit.org/nst/cgi-bin/ip.cgi"

r$ = httpget$(url$)
print r$
end
This demo retrieves the index page at http://libertybasic.com and prints the result.
'Retrieve the webpage at libertybasic.com 
'and print it.
print httpget$("http://libertybasic.com")
The next demo assigns the contents of the "What's New" Liberty BASIC page to a string variable, then prints that variable.
'The function can also be used this way.
'The web page news.html is assigned to the
'string variable stuff$, then printed
stuff$ = httpget$("http://libertybasic.com/news.html")
print stuff$
Other kinds of files can be retrieved with this function. The following demo, modified from one that was shared by Carl, retrieves an image file with HTTPGET$(). It then creates a binary file on disk and writes the results of HTTPGET$() to disk. The disk file image is then loaded with LOADIMAGE and displayed in a graphic object.
'To retrieve an image file, assign it to
'a string variable:
pic$ = httpget$("http://libertybasic.com/lb3banner.jpg")

'Create a disk file, then write the 
'image information to disk.
open "lb3banner.jpg" for binary as #1
print #1, pic$
close #1

'Load the image and display it in a graphic object.
loadimage "banner", "lb3banner.jpg"
graphic #g, 500, 100
#g drawimage("banner",0,0)
render #g

Useful Procedures

' Place a useful function or sub using this keyword here