Skip to Content
Menu
Dette spørgsmål er blevet anmeldt
4 Besvarelser
10775 Visninger

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
Kassér
Bedste svar

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
Kassér
Bedste svar

Hello,

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

Thanks 

Avatar
Kassér
Bedste svar

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
Kassér
Forfatter Bedste svar

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

Avatar
Kassér
Related Posts Besvarelser Visninger Aktivitet
3
aug. 20
10478
0
jul. 17
3225
2
aug. 20
8077
2
aug. 24
1434
0
jan. 25
1317