Passing Parameters to Web Dev using system.net.httpGet()

I’m using a Web Dev doGet() script to grab a filestream from an SQL database that is then displayed using a PDF viewer component. Since the list of files changes as users upload more files to the database, I can’t just make a different doGet() for every single file. Instead, I want to use a dropdown to select the file name from a list, then use the a query in the doGet functions that will grab the filestream for that corresponding name.

I’m running the doGet script from a push button using a system.net.httpGet() function. The only part I’m missing is how to pass a parameter from the push button script to the Web Dev doGet() function.

I think this can be done by passing a parameter through the headerValues parameter in the system.net.httpGet() function, but I’m not entirely sure how HTTP headers work.

Thanks for any help.

If it’s a GET request, you probably want to use URL parameters. There’s no easy way to do that with httpGet, but if you’re on 8.0.6+ you can use system.net.httpClient; something like:

url = "gatewayip:8088/system/webdev/project/api"
params = {"file": "keyIdentifyingFile"}
pdfBytes = system.net.httpClient().get(url, params=params).body

If you’re on an earlier version, the same technique would still work, you’ll just have to manually encode the URL parameters; see https://en.wikipedia.org/wiki/Query_string. There’s built-in functions in python’s urllib module that can take of doing the encoding, but you’ll have to import them.

I’m using 8.0.10-rc1 so I can take advantage of the report viewer.

Will running the system.net.httpClient().get(url, params=params).body update the pdfBytes that the Web Dev script is returning? I currently use the system.db.runScalarPrepQuery() inside the Web Dev script, so this is where i need the parameters passed to.

How do I go about using the parameter inside of the Web Dev script once my button script has been executed?

This is my runAction() script for the button

    url = "http://localhost:8088/system/webdev/my_project/get_PDF"
	params = {"file": "foo.pdf"}
	pdfBytes = system.net.httpClient().get(url, params=params).body

This is the doGet() script in my get_PDF Web Dev funtion

    fileName = data['file']
    data = system.db.runScalarPrepQuery("SELECT file_stream FROM Files WHERE name = ?", [fileName])
    return {'contentType':'application/pdf', 'bytes':data}

Since you’re passing the filename as a URL parameter, you would retrieve it the same way; so in your webdev script, just change data['file'] to params['file'] and it’ll be automatically retrieved from the URL parameters.

Thanks for the help so far, but sadly I still haven’t had any luck getting this working.

I changed my Web Dev doGet script to be the following based on some stuff I saw in the OnlineDemo project for Ignition 8.0

	params = request['params'] if 'params' in request else {}
	if 'file' in params:
		fileName = params['file']
	else:
		fileName = 'Foo.pdf'
	data = system.db.runScalarPrepQuery("SELECT file_stream FROM Files WHERE name = ?", [fileName])
	return {'contentType':'application/pdf', 'bytes':data}

This way, the httpGet script will open with the default Foo.pdf even if the button hasn’t been clicked to pass parameters to the Web Dev URL.

However, when I click the button to set the new parameters, the PDF viewer on my page doesn’t change at all.

Is it possible that I need to refresh the component somehow after passing the parameters, or invoke the Web Dev script after updating the parameters so that the PDF being displayed is updated?

How are you actually calling the HTTP request? A propertyChange script? Can you show what you’re doing/the code to make the request?

Sorry, I dont’t know if the HTTP request is the Web Dev doGet script, or the system.net.httpClient()

My only scripting is an “onClick” event from a pushbutton that runs the following script

    url = 'http://localhost:8088/system/webdev/my_project/get_PDF'
	params = {'file': 'File 2.pdf'}
	pdfBytes = system.net.httpClient().get(url, params=params).body

and a Web Dev doGet() script called get_PDF with the following code

    params = request['params'] if 'params' in request else {}
	if 'file' in params:
		fileName = params['file']
	else:
		fileName = 'File 1.pdf'
	data = system.db.runScalarPrepQuery("SELECT file_stream FROM Files WHERE name = ?", [fileName])
	return {'contentType':'application/pdf', 'bytes':data}

Then a PDF viewer on the same view as the pushbutton with the source property set to

/system/webdev/my_project/get_PDF

If i understand this correctly, when my page loads and the PDF viewer opens the /system/webdev/my_project/get_PDF url, it should display File 1.pdf since the parameters are empty, then when the button is clicked, the parameter File 2.pdf should be passed to the Web Dev page, and File 2 should be displayed.

Do I need to call the Web Dev script from the bush button by adding a line that says something line doGet(get_PDF)?

Okay, so you’re actually sort of overcomplicating the process - which is good news, because it means you can do this a lot easier :).
The PDF viewer in Perspective doesn’t work quite like the PDF viewer in Vision - you can’t (to my knowledge) directly populate it with a byte array. Instead, what you need to do is modify the source property to have a different URL, and then the component itself will automatically issue the HTTP request and render the returned PDF bytes. So, something like this, substituting your own property in the urlEncode() expression function call:


stringFormat("/system/webdev/my_project/get_PDF?file=%s", urlEncode({parent.props.mode}))
(You need to use the urlEncode function so that any spaces or special characters in the filename provided are ‘escaped’ into a valid URL)

Whenever you change the referenced property (in my example, {parent.props.mode}), the expression will automatically update the source URL - and then the PDF viewer component will automatically “know” it needs to fetch a new PDF to display.

1 Like

Glad to hear that I’ve been over complicating this!

Thank you so much for your help, my issue is taken care of!