What I’m trying to do:
I’m trying to send a POST request to a Spring Boot app running on my localhost. What I’ve tried and what’s not working:
The url I’m trying to make a POST to is “http://localhost:8080/connect”, but I receive the error “anvil.http.HttpRequestFailed: Connection refused”. Thanks very much for everyone who will help. Code Sample:
# The function running on Anvil server
@anvil.server.callable
def connect(url):
try:
request(url, "POST")
return True
except HttpError as e:
return False
# The function running on Anvil client
def connect_button_click(self, **event_args):
"""This method is called when the button is clicked"""
# The variable self.coordinator_ip_address.text should be "localhost"
url = f"http://{self.coordinator_ip_address.text}:8080/connect"
connected = anvil.server.call('connect', url)
if(connected):
alert("Connected successfully")
else:
alert("Connection failed")
From the Anvil server point of view, localhost is the server itself, not your computer.
If you want the server to run some code on your computer you can use the uplink, much easier than setting up some http endpoints.
If your local code is an already existing app with http endpoints, you could have the Anvil server call uplink functions running on your computer and an uplink script on your pc that accesses localhost.
Thanks for the answers to all of you.
My local application is a Spring Boot written in Java and I would like to make REST API calls to its function from my Anvil App.
How can I do?
If your local Java API server is not publicly visible over the internet then @stefano.menci gives a good suggestion in the uplink mechanism. Please read about it here :
You could call the Java API from within your uplink code so long as you run them both on the same server (again assuming your Java API is not publicly visible).
which implies your Java API app is running on your laptop or desktop (or on a computer within your own private network/office) and not on a computer that is accessible (or visible) over the public internet.
If that is true, then any code you write that runs on Anvil’s hosted servers will not be able to contact your API (unless you have somehow made it public, which I doubt).
Yes, it’s true. I understand.
I understood that python Anvil server code now must run on my local machine and must be connected via Uplink but I cannot find anvil.http library on my local machine (I’ve already installed python Anvil library)
No, keep it simple. The Anvil server is in uk, it can see any public ip address, it can’t see your localhost, it can see your uplink.
So:
create a script on your pc that calls your localhost http endpoints and returns whatever you need. When this works…
… go to your app, enable the uplink, get the uplink key, add the snippet of code from the uplink settings to your local script, add the server.callable decorator to the function you tested earlier, and run it. At this point you have a local script connected to your app and waiting for the app to call any decorated function
in the app replace the http requests with server.call to call your uplink functions
I thank all you guys. Finally this solution you provided is ok for me.
I have another question: is there another method to take the code for the GUI generated by Anvil client and put it onto the local machine and run the front-end on the local machine?