Overslaan naar inhoud
Menu
Je moet geregistreerd zijn om te kunnen communiceren met de community.
Deze vraag is gerapporteerd
2 Antwoorden
2168 Weergaven

Hi All,

I am trying out webhooks and watch a few youtube clips including Kevin Zaki's awesome vid.

I have followed along however I keep getting the same error: NameError: name 'request' is not defined. 

Does anyone know how to fix?

Thank you for reading.

Avatar
Annuleer

can you post your code or video that you reference?

Auteur

Sure here is the vid: https://youtu.be/wyU0SjOQXRk

The target record is: model.browse(int(payload.get('id'))

The code to execute is:
try:
data = request.get_json_data()
except:
data = str(request.get_http_params())

id = data['id']
message = data['message']

env['mail.message'].create({
'author_id': 2,
'model': 'sale.order',
'res_id': id,
'body': message,
'message_type': 'comment',
'subtype_id': 2,
})

The postman xml:
{
"id": 1,
"message": "Hello"
}

dunno mate. I try following the tutorial. It's working fine. No "NameError: name 'request' is not defined"

All I can suggest is try check your odoo 17 base addons/base_automation/models/base_automation.py

check if "from odoo.http import request" is on the top. if it doesn't exist, add it manually (because mine is exist). or you can download the odoo again (because it's fresh so the bug should be fixed)

someone might accidentally delete those line or you download when this is still issue (idk i'm not maintaining the odoo)

after that, try restart the server and upgrade "Automation Rules"

Beste antwoord

Hi,
As per the shared video, the website app has to be installed in order to get the request library.

So if its fine for you, install the website app and see.

Update:
it is not necessary to install the website app, but the code can be changed as follows:

id = payload['id']

message = payload['message']


env['mail.message'].create({

'author_id': 2,

'model': 'sale.order',

'res_id': id,

'body': message,

'message_type': 'comment',

'subtype_id': 2,

})



Thanks

Avatar
Annuleer
Auteur

Doh.... Yep, that was it and Yep, he did say that... Once again you have nailed it Niyas.

On further checking, it is not mandatory to install website app, you can get the passed data from the payload variable

Beste antwoord

Hi,

To resolve the issue make sure you've imported the 'request' correctly.

from odoo import http

from odoo.http import request


Make sure that your controller class inherits from http.Controller. The request object is only available within the HTTP request handling methods of this class. After making the changes to your Odoo module, restart the Odoo server to ensure the changes take effect.


Hope it helps

Avatar
Annuleer
Auteur

I appreciate the reply but, this is a webhook. Not a controller. This is executing code as part of an automation.

Gerelateerde posts Antwoorden Weergaven Activiteit
1
dec. 22
2844
1
okt. 24
1953
2
aug. 24
1049
1
mrt. 24
2132
1
feb. 24
2422