This question has been flagged
4 Replies
7776 Views

Hey!

I've read this post: https://www.odoo.com/es_ES/forum/ayuda-1/post-to-external-url-from-server-action-99309 and asked for some help there but got none, also I cannot continue the conversation there since i don't have enough karma. My question is how can i do an external HTTP request on a Server Action?, I've tried with Python Requests like:

```
requests.get("http://example.com")
```

but i get:

ValueError: <class 'NameError'>: "name 'requests' is not defined" while evaluating

I've tried installing urllib3 with: `sudo pip install urllib3` but looks like it is already installed:

`Requirement already satisfied: urllib3 in /opt/bitnami/python/lib/python3.7/site-packages/urllib3-1.22-py3.7.egg`

thanks!

Avatar
Discard
Best Answer

Hello,

In server actions I don't believe this is possible, when using requests you need to import the requests python library.

I have found the best way to achieve what you need is to create a custom module with a model that extends the model you want the server action to run on and define a function. Eg Def call_endpoint(self):

Then you can call this function in the server action you call this function using record.call_endpoint() and you request call will be in this function.

If you need further help, let me know,

Thanks,

Avatar
Discard
Best Answer

Hello,

I have the same problem, can someone help me please?

Thanks 

Avatar
Discard
Best Answer

The only Python libraries you can access in a Server Action are: datetime, dateutil, time, timezone.


See https://www.odoo.com/documentation/14.0/reference/actions.html#evaluation-context - to use other libraries, write a module that creates an action that you can then call, review the base_geolocalize module for an idea (this module leverages Open StreetMap)

Avatar
Discard
Author Best Answer

Thanks @Jack Dane, i did what you suggested and it worked!

Avatar
Discard