Skip to content

Request body is already encoded as UTF-8 #24

@stanier

Description

@stanier

Upon receiving a webhook of default templating from Netbox v3.1.7, the following error is generated otherwise causing silent failure, on a fresh install.

The request data returned by request.get_data() appears to now be returning the request body encoded for UTF-8 by default, possibly as a new behavior of flask.

While I was not able to track down this exact behavior whilst skimming Flask's changelogs, I did find that they had done a lot of deprecation and other work with request JSON encoding/decoding. Referring to the documentation, https://tedboy.github.io/flask/generated/generated/flask.Request.get_data.html mentions an as_text flag for the function request.get_data(), saying this:

If as_text is set to True the return value will be a decoded unicode string

Amending the flag to the request.get_data() invocation parameters at line 34 of sensors/netbox_webhooks.py to become request.get_data(as_text=True) appears to mitigate the issue. This has been tested in production staging and appears to resolve it. Will be opening a PR to address this issue promptly.

2022-03-14 20:13:04,333 139891243266120 INFO _internal [-] 1.1.1.1 - - [14/Mar/2022 20:13:04] "ESC[35mESC[1mPOST /netbox/webhooks
/ HTTP/1.1ESC[0m" 500 -
2022-03-14 20:14:12,302 139891243266120 ERROR app [-] Exception on /netbox/webhooks/ [POST]
Traceback (most recent call last):
  File "/opt/stackstorm/virtualenvs/netbox/lib/python3.6/site-packages/flask/app.py", line 2073, in wsgi_app
    response = self.full_dispatch_request()
  File "/opt/stackstorm/virtualenvs/netbox/lib/python3.6/site-packages/flask/app.py", line 1518, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/opt/stackstorm/virtualenvs/netbox/lib/python3.6/site-packages/flask/app.py", line 1516, in full_dispatch_request
    rv = self.dispatch_request()
  File "/opt/stackstorm/virtualenvs/netbox/lib/python3.6/site-packages/flask/app.py", line 1502, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "/opt/stackstorm/packs/netbox/sensors/netbox_webhooks.py", line 34, in event
    msg=request.get_data().encode('utf8'),
AttributeError: 'bytes' object has no attribute 'encode'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions