Web search macro using POST method

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
paul1149
Posts: 33
Joined: Mon Jun 27, 2016 12:56 am

Web search macro using POST method

Post by paul1149 »

Hello,

I have a series of macros that search the Web for the current selected text. Here's an example:

Code: Select all

Sub Brave_Lookup : site = "https://search.brave.com/search?q=" : Web_Lookup (site, token) : End Sub 

	Function Web_Lookup (site, token)
	 	oVC = ThisComponent.CurrentController.getViewCursor
		oTC = oVC.getText().createTextCursorByRange(oVC)
		oSel = oTC.string ' get selected text
		Dim SysShell As Object
   	        SysShell = createUNOService("com.sun.star.system.SystemShellExecute")
                SysShell.execute("/usr/bin/vivaldi-snapshot", site & oSel & token, 0) 'vivaldi-snapshot
	End Function
I've simplified that and hopefully have not removed anything necessary. It does work perfectly.

What I want to do now is have a macro that invokes a POST web search. Here's what I'm trying to do:

Code: Select all

https://www.blueletterbible.org/search/preSearch.cfm{POSTARGS}Criteria=%s&t=NASB95&csr=0&csrf=0&csrt=0&cscs={POSTENCODING}UTF-8
I suspect it's simple, but I don't know how to break this down and pass the extra arguments to the Web_Lookup function.

Am I going about this the right way? Thanks!
LibreOffice 24.2.4.2, Linux
User avatar
john125wheeler
Banned
Posts: 2
Joined: Mon Jul 08, 2024 7:58 am

Re: Web search macro using POST method

Post by john125wheeler »

Hello, @paul1149

Your approach is on the right track! To perform an HTTP POST request from your macro, you’ll need to modify your Web_Lookup function to handle POST requests. Let’s break it down:

Construct the URL: You’ve already defined the URL for your POST request:

Code: Select all

https://www.blueletterbible.org/search/preSearch.cfm{POSTARGS}Criteria=%s&t=NASB95&csr=0&csrf=0&csrt=0&cscs={POSTENCODING}UTF-8
Replace {POSTARGS} with the actual arguments you want to send (e.g., search criteria), and {POSTENCODING} with the appropriate encoding (usually application/x-www-form-urlencoded).
Create the HTTP request: You can use WinHttp.WinHttpRequest.5.1 to send the POST request. Here’s how you can modify your Web_Lookup function:

Code: Select all

Function Web_Lookup(site As String, token As String)
    Dim http As Object
    Set http = CreateObject("WinHttp.WinHttpRequest.5.1")
    
    ' Construct the URL with your POST arguments
    Dim url As String
    url = "https://www.blueletterbible.org/search/preSearch.cfm" & _
          "?Criteria=" & oSel & "&t=NASB95&csr=0&csrf=0&csrt=0&cscs=UTF-8"
    
    ' Open the request
    http.Open "POST", url, False
    
    ' Set any additional headers if needed (e.g., User-Agent)
    http.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
    
    ' Send the request
    http.send ""
    
    ' Handle the response (you can access it via http.responseText)
    ' For example, display it in a message box:
    MsgBox http.responseText
End Function
Invoke the function: Call your modified Web_Lookup function with the appropriate site and token values.
Remember to replace placeholders like {POSTARGS} and {POSTENCODING} with actual values relevant to your search. If you need to pass specific data (e.g., JSON payload), adjust the http.send line accordingly.

Feel free to adapt this example to your specific use case.

Hope this work for you.
Best Regards,
john125wheeler

User banned (post edited to add a spam link. Post however kept since the code may be of some help (Hagar, Moderator).
Last edited by john125wheeler on Tue Jul 09, 2024 5:50 am, edited 1 time in total.
OpenOffice 3.1 on Windows Vista | NeoOffice 2.2.3 with MacOS 10.4 | OpenOffice 2.4 on Ubuntu 9.04
paul1149
Posts: 33
Joined: Mon Jun 27, 2016 12:56 am

Re: Web search macro using POST method

Post by paul1149 »

Thanks. That sounds like what I need. I'm getting an error on this line:

Code: Select all

Set http = CreateObject("WinHttp.WinHttpRequest.5.1")

Code: Select all

Error creating object.
Module cannot be loaded; invalid format.
I'm on LO 24.2.4.2 on Linux.
LibreOffice 24.2.4.2, Linux
ms777
Volunteer
Posts: 208
Joined: Mon Oct 08, 2007 1:33 am

Re: Web search macro using POST method

Post by ms777 »

WinHttp.WinHttpRequest.5.1 just works on Windows machines
ms777
Volunteer
Posts: 208
Joined: Mon Oct 08, 2007 1:33 am

Re: Web search macro using POST method

Post by ms777 »

in Python, you could probably use the https://www.w3schools.com/python/ref_requests_post.asp method. I am not aware of something similar in AO Basic
User avatar
karolus
Volunteer
Posts: 1236
Joined: Sat Jul 02, 2011 9:47 am

Re: Web search macro using POST method

Post by karolus »

ms777 wrote: Mon Jul 08, 2024 9:36 pm in Python, you could probably use the https://www.w3schools.com/python/ref_requests_post.asp method. I am not aware of something similar in AO Basic
»requests« has a page on pypi and its own homepage and documentation no need to link random websites!
Libreoffice 25.2… on Debian 13 (trixie) (on RaspberryPI5)
Libreoffice 25.8… flatpak on Debian 13 (trixie) (on RaspberryPI5)
paul1149
Posts: 33
Joined: Mon Jun 27, 2016 12:56 am

Re: Web search macro using POST method

Post by paul1149 »

Thanks. I'm completely unfamiliar with python and how to use it in LO.

I have a GET search set up for that site, and it works fine, but only on word and phrase searches. It breaks up Bible verse addresses into their components and return, for example, "2 and Peter and 1:13 not found" for a search for "2 Peter 1:13", whereas that search at the site itself works fine. So I thought doing the remote search via POST could be the answer. It's been a while now, but I think at the time I found the search field uses POST.
LibreOffice 24.2.4.2, Linux
Cormic
Posts: 10
Joined: Tue Jul 16, 2024 1:50 pm

Re: Web search macro using POST method

Post by Cormic »

Hello,
I have a solution to execute a POST request in Basic avoiding any external library.

It uses UNO Universal Content Broker, and works under Windows and Linux (I didn't test Mac OS)

An exemple is in the Calc attached file, in the module HTTPmodule.
The example URL (line 4 : POST_URL = "http://mco.s2hnh.org/") is functional (but not forever :-)).
Clicking on the sheet button "Tester l'envoi POST" will run the subroutine Main, which will display the POST answer.
Due to a former bug in UCB, Linux LibreOffice may send PUT request instead of POST. This bug has been corrected (in LibreOffice version 6 or 7, I am not sure). The POST answer from http://mco.s2hnh.org/ will show the parameters received (should be in MIME format application/x-www-form-urlencoded) and indicate if it was a PUT or a POST answer.

Hope it will be usefuL
Sincerely
Attachments
Test_HTTP_POST.ods
(12.89 KiB) Downloaded 155 times
LibreOffice 7.5.12 Windows 10
paul1149
Posts: 33
Joined: Mon Jun 27, 2016 12:56 am

Re: Web search macro using POST method

Post by paul1149 »

Thanks very much, @Cormic. I greatly appreciate the effort. I will take a look at this.
LibreOffice 24.2.4.2, Linux
User avatar
Jurassic Pork
Posts: 31
Joined: Wed Oct 25, 2017 7:55 am
Location: France

Re: Web search macro using POST method

Post by Jurassic Pork »

Hello,
same result as Basic with this python macro :

Code: Select all

def HttpPostTest(*args):
    from urllib import request, parse
    url = 'http://mco.s2hnh.org/'
    data = {'A':'varA','B':'varB'}
    data = parse.urlencode(data).encode()
    req = request.Request(url, data=data)
    response = request.urlopen(req)
    MsgBox(response.read())
urllib is included in the python of LibreOffice

in Attachment a workbook with the Basic Code and the Python Code (embedded) . Works only with LibreOffice (not compatible with Python 2.7 OpenOffice)

Friendly, J.P
Attachments
Test_HTTP_POST_Python.ods
(15.5 KiB) Downloaded 122 times
OpenOffice 4.1.14 , LibreOffice 7.6.2.1 on Windows 11/ LibreOffice 7.3.7 on Lubuntu 22.04
Cormic
Posts: 10
Joined: Tue Jul 16, 2024 1:50 pm

Re: Web search macro using POST method

Post by Cormic »

Hello Jurassic Pork,
Thanks for the code.
Obviously, Python has some advantages compared to Basic which is really an old language, lacking a lot of modern functionnalities.
Its main advantage is that it is directly accessible into LibreOffice.
I should consider studying a bit of Python to compare with Java, which is more familiar to me.
Sincerely, Cormic
LibreOffice 7.5.12 Windows 10
User avatar
karolus
Volunteer
Posts: 1236
Joined: Sat Jul 02, 2011 9:47 am

Re: Web search macro using POST method

Post by karolus »

Hallo

@J.P: LO comes with a msgbox-modul!

Code: Select all

from urllib import request, parse
from msgbox import MsgBox, ok, cancel, SIMPLE

def messagebox( message, deko=SIMPLE, title="Read_ME" ):
    msg = MsgBox( XSCRIPTCONTEXT.ctx )
    msg.addButton( ok )
    msg.addButton( cancel )
    msg.show( message, deko, title)
    
def HttpPostTest(*args):    
    url = 'http://mco.s2hnh.org/'
    data = {'A':'varA','B':'varB'}
    data = parse.urlencode(data).encode()
    req = request.Request(url, data=data)
    response = request.urlopen(req)
    message = response.read().decode("utf8")
    message = message.replace('.','\n')
    messagebox(f"{message}")
Libreoffice 25.2… on Debian 13 (trixie) (on RaspberryPI5)
Libreoffice 25.8… flatpak on Debian 13 (trixie) (on RaspberryPI5)
ms777
Volunteer
Posts: 208
Joined: Mon Oct 08, 2007 1:33 am

Re: Web search macro using POST method

Post by ms777 »

@Cormic, excellent piece of code! I suggest that you add the code to your post, just that it can be easier found when somebody does a search in this forum to the functions / services you use.
One minor suggestion: You can remove the XActiveDataSink listener and replace

Code: Select all

gResponseListener = CreateUnoListener("XActiveDataSink_",    "com.sun.star.io.XActiveDataSink")
by

Code: Select all

responseStream = CreateUnoService("com.sun.star.io.Pipe")
and modify the rest accordingly. This also works
Thanks for this code,
ms777
paul1149
Posts: 33
Joined: Mon Jun 27, 2016 12:56 am

Re: Web search macro using POST method

Post by paul1149 »

Thank you, Jurassic. I'll give this a look too. I'm particularly interested in the Python aspect.

I did reply earlier to you, but I see it didn't make it through.
LibreOffice 24.2.4.2, Linux
Cormic
Posts: 10
Joined: Tue Jul 16, 2024 1:50 pm

Re: Web search macro using POST method

Post by Cormic »

Thanks @ms777 for the comment. It's always nice to see your work appreciated.

I have tried your second suggestion, using

Code: Select all

responseStream = CreateUnoService("com.sun.star.io.Pipe")
but it didn't work on my machine (Win10 pro, LO 24.5.2.2).
The listener function XCommandEnv_getInteractionHandler() gets called, then XInteractionHandle_handle() and Basic send me to the error handler at label Err:

Following your first suggestion, I have created a specific post under the title "How to send an HTTP POST request from a Basic macro" in the "Code snippets" section.
Sincerely
Cormic
LibreOffice 7.5.12 Windows 10
Post Reply