Web 2 HandleSpecialURL Equivalent?

Is there an equivalent way to handle GET and POST in Web 2? Everything I have tried in Web 2 Fails. Here is working code in a Web 1

Dim ReqMethod,ReqString As String

ReqMethod = Uppercase(Request.Method)
ReqString = Request.QueryString

Select Case Lowercase(Request.Path)
  
Case "folderdata"
  If ReqMethod = "GET" Then
    LogTransactions("GET Received From: " + Request.RemoteAddress )
    LogTransactions("Query String Received: + ReqString)
       // Return true to signal that we handled the request
    Return True
  Else
    //Return false since we only handle POSTs
    Return False
  End If
  
Else
  // Don't handle any other requests
  LogExceptions("Request Path " + ReqMethod + " not handled")
  LogTransactions("Request Path " + ReqMethod + " not handled")
  Return False
End Select

Sending thiis GET returns 200 OK

http://127.0.0.1:8080/special/folderdata?barcode=Y2022J040060S02M003Z8B1502262OR&folderid=09&input=273&output=270

and This is the Transaction log UI output

2022-08-18 14:10:27 Web Service Log Created


2022-08-18 14:10:27 GET Received From: 127.0.0.1
2022-08-18 14:10:27 Query String Received: barcode=Y2022J040060S02M003Z8B1502262OR&folderid=09&input=273&output=270

How do I do this in Web 2.0. What I have tried just gives me a 404 Not Found using the following GET:

http://127.0.0.1:8080/folderdata?barcode=Y2022J040060S02M003Z8B1502262OR&folderid=09&input=273&output=270

Trying to show the query string in a TextArea gives a nil object exception and just storing the query string in a variable stores the string but it still returns a 404 Not Found

Function HandleURL(Request As WebRequest, Response As WebResponse) Handles HandleURL as Boolean
  Var bool As Boolean
  Var ReqMethod,ReqString As String
  
  ReqMethod = Uppercase(Request.Method)
  ReqString = Request.QueryString
  
  Select Case Lowercase(Request.Path)
    
  Case "folderdata"
    If ReqMethod = "GET" Then
      App.QryStrng = ReqString
      // Return true to signal that we handled the request
      Return True
    Else
      // Return false since we only handle GET
      Return False
    End If
  Else
    // Return false since it's the wrong folder
    Return False
  End Select
End Function

Screen capture from Talend Chrome plugin

You need to set up the response before returning true.

Response.Status = 200
Response.Write("Ok")
1 Like

I’m getting that Response doesn’t exist when compiling. I’ve tried various things like Request.Response and WebRequest.Response but they don’t have a Status or Write property.

Just type what Wayne said to use. Don’t add Request before anything.

As I stated before the compiler says it doesn’t exist

Response is a variable passed to you via the Event. The compiler saying it doesn’t exist might mean that you’re writing code somewhere that doesn’t have the Response object.

Here’s a quick sample project that might help you see what’s going wrong: HandleURL.xojo_xml_project

uh… Use the HandleURL event not HandleSpecialURL. (HandleSpecialURL doesn’t exist in web 2)

DOH! I was adding that to the wrong project, (FACEPALM!) It works in the Web2 project.

Thanks guys.

2 Likes

One other thing. If I try to use the QueryString in a Text Field I get a Nil Object Exception. I’ve tried storing it in a variable first also and it stores fine but I still get Nil Object Exception if I try to use the variable from the HandleURL event.

Function HandleURL(Request As WebRequest, Response As WebResponse) Handles HandleURL as Boolean
  Var ReqMethod,ReqString As String
  
  ReqMethod = Uppercase(Request.Method)
  
  Select Case Lowercase(Request.Path)
    
  Case "folderdata"
    If ReqMethod = "GET" Then
      WebPage1.TextField1.Text = Request.QueryString
      Response.Status = 200
      Response.Write("Ok")
      // Return true to signal that we handled the request
      Return True
    Else
      // Return false since we only handle GET
      Return False
    End If
  Else
    // Return false since it's the wrong folder
    Return False
  End Select
End Function

There is no session yet in HandleURL, that’s why you’re getting the NilObjectException.

Apparently the way to get Query Parameters from an active page is to parse the URL yourself: WebSession footnote about Query Parameters

Hmmmm… That is ringing a bell. I think I handled this in the past with a timer in the web page to read a variable to populate the text area.

1 Like

While doing what you want does require an excessive amount of bending over backwards (you can’t start a session anywhere other than the root), I don’t think it’s ever required a Timer.

Here’s an updated version of the HandleURL project that shows you how to get the parameters into a TextField on a WebPage.

However, this does not get into dealing with specific paths like /folderdata/ because that’s a nightmare to do in Xojo Web.

Updated HandleURL.xojo_xml_project

The timer just updates the log entries on the web page if you are viewing the page. Otherwise the GET just runs in the background triggering app level stuff. In this case it will save the data as a JSON file for another system to pick up at regular intervals. I have to use a GET because the PLC board doesn’t support a POST with a JSON payload.

Ah, so the Timer was specific to what your needs were, gotcha.