Passa al contenuto
Menu
È necessario essere registrati per interagire con la community.
La domanda è stata contrassegnata
4 Risposte
10715 Visualizzazioni

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
Abbandona
Risposta migliore

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
Abbandona
Risposta migliore

Hello,

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

Thanks 

Avatar
Abbandona
Risposta migliore

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
Abbandona
Autore Risposta migliore

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

Avatar
Abbandona
Post correlati Risposte Visualizzazioni Attività
3
ago 20
10429
0
lug 17
3175
2
ago 20
8038
2
ago 24
1403
0
gen 25
1266