masonite icon indicating copy to clipboard operation
masonite copied to clipboard

Content-Type: text/plain Not supported

Open bikash-kodiary opened this issue 1 year ago • 5 comments

Describe the bug

simple post request to the controller with content-type: text/plain error:

[ERROR] Error handling request /api/test 2024-10-24 15:32:27 Traceback (most recent call last): 2024-10-24 15:32:27 File "/usr/local/lib/python3.8/site-packages/masonite/app.py", line 227, in resolve 2024-10-24 15:32:27 return obj(*objects) 2024-10-24 15:32:27 File "/usr/local/lib/python3.8/site-packages/masonite/providers/AppProvider.py", line 61, in boot 2024-10-24 15:32:27 self.app.bind("Request", Request(self.app.make("Environ")).load_app(self.app)) 2024-10-24 15:32:27 File "/usr/local/lib/python3.8/site-packages/masonite/request.py", line 67, in init 2024-10-24 15:32:27 self.load_environ(environ) 2024-10-24 15:32:27 File "/usr/local/lib/python3.8/site-packages/masonite/request.py", line 279, in load_environ 2024-10-24 15:32:27 environ["POST_DATA"] = self.get_post_params() 2024-10-24 15:32:27 File "/usr/local/lib/python3.8/site-packages/masonite/request.py", line 317, in get_post_params 2024-10-24 15:32:27 fields = cgi.FieldStorage( 2024-10-24 15:32:27 File "/usr/local/lib/python3.8/cgi.py", line 477, in init 2024-10-24 15:32:27 self.read_single() 2024-10-24 15:32:27 File "/usr/local/lib/python3.8/cgi.py", line 670, in read_single 2024-10-24 15:32:27 self.read_binary() 2024-10-24 15:32:27 File "/usr/local/lib/python3.8/cgi.py", line 692, in read_binary 2024-10-24 15:32:27 self.file.write(data) 2024-10-24 15:32:27 TypeError: write() argument must be str, not bytes 2024-10-24 15:32:27 2024-10-24 15:32:27 The above exception was the direct cause of the following exception: 2024-10-24 15:32:27 2024-10-24 15:32:27 Traceback (most recent call last): 2024-10-24 15:32:27 File "/usr/local/lib/python3.8/site-packages/masonite/wsgi.py", line 27, in response_handler 2024-10-24 15:32:27 container.resolve(provider.boot) 2024-10-24 15:32:27 File "/usr/local/lib/python3.8/site-packages/masonite/app.py", line 232, in resolve 2024-10-24 15:32:27 raise exception(str(e) + " while calling {}".format(obj)) from e 2024-10-24 15:32:27 masonite.exceptions.ContainerError: write() argument must be str, not bytes while calling <bound method AppProvider.boot of <masonite.providers.AppProvider.AppProvider object at 0xffff896e62b0>> 2024-10-24 15:32:27 2024-10-24 15:32:27 During handling of the above exception, another exception occurred: 2024-10-24 15:32:27 2024-10-24 15:32:27 Traceback (most recent call last): 2024-10-24 15:32:27 File "/usr/local/lib/python3.8/site-packages/gunicorn/workers/sync.py", line 136, in handle 2024-10-24 15:32:27 self.handle_request(listener, req, client, addr) 2024-10-24 15:32:27 File "/usr/local/lib/python3.8/site-packages/gunicorn/workers/sync.py", line 179, in handle_request 2024-10-24 15:32:27 respiter = self.wsgi(environ, resp.start_response) 2024-10-24 15:32:27 File "/usr/local/lib/python3.8/site-packages/whitenoise/base.py", line 86, in call 2024-10-24 15:32:27 return self.application(environ, start_response) 2024-10-24 15:32:27 File "/usr/local/lib/python3.8/site-packages/masonite/wsgi.py", line 29, in response_handler 2024-10-24 15:32:27 container.make("ExceptionHandler").load_exception(e) 2024-10-24 15:32:27 File "/usr/local/lib/python3.8/site-packages/masonite/app.py", line 109, in make 2024-10-24 15:32:27 raise MissingContainerBindingNotFound( 2024-10-24 15:32:27 masonite.exceptions.MissingContainerBindingNotFound: ExceptionHandler key was not found in the container

This is the curl format for that request:

curl --location 'http://localhost:8000/api/test' \
--header 'Content-Type: text/plain' \
--header 'Cookie: csrf_token=gAAAAABnFjJj_tfb8Bw0Kxqmu0Bxsjp8qcXCDAQUYw5AFVChfu2hqGWweXUmFk4yqtET0O_SyOKK2HOxmY-ED7LEWrZuUjAccW91ZwimIyYxmL6zYQMYNM4=' \
--data 'asd asd asd asd asd asd asd asd as dasd'

Controller code: `class TestController(Controller): """TestController Controller Class."""

def __init__(self, request: Request, response: Response,view:View):
    """TestController Initializer

    Arguments:
        request {masonite.request.Request} -- The Masonite Request class.
    """
    self.request = request
    self.response = response
    self.view = view
    

def textCheck(self):
    print(self.request.all())`

web.py code: Post("/api/text", "Api.TestController@textCheck")

Expected behaviour

I except to get the posted text data posted to the url.

Steps to reproduce the bug

No response

Screenshots

No response

OS

Linux

OS version

sonama 14.3

Browser

No response

Masonite Version

4.6.1

Anything else ?

No response

bikash-kodiary avatar Oct 24 '24 09:10 bikash-kodiary

I want to use it as the third party api provider post back the response to our server which is in plain text . This is what i've done it on fastApi:

async def echo_request_body(request: Request):
    body = await request.body()
    decoded_json = json.loads(base64.b64decode(body).decode())

bikash-kodiary avatar Oct 24 '24 10:10 bikash-kodiary

How were you doing this in Masonite? what did the Masonite controller look like? i need to know the exact data type you were returning i think

josephmancuso avatar Oct 24 '24 12:10 josephmancuso

Also the request data because it looks from the stack trace its throwing an error trying to load the post data into the request class

2024-10-24 15:32:27 File "/usr/local/lib/python3.8/site-packages/masonite/request.py", line 279, in load_environ
2024-10-24 15:32:27 environ["POST_DATA"] = self.get_post_params()
2024-10-24 15:32:27 File "/usr/local/lib/python3.8/site-packages/masonite/request.py", line 317, in get_post_params
2024-10-24 15:32:27 fields = cgi.FieldStorage(
2024-10-24 15:32:27 File "/usr/local/lib/python3.8/cgi.py", line 477, in init
2024-10-24 15:32:27 self.read_single()
2024-10-24 15:32:27 File "/usr/local/lib/python3.8/cgi.py", line 670, in read_single
2024-10-24 15:32:27 self.read_binary()
2024-10-24 15:32:27 File "/usr/local/lib/python3.8/cgi.py", line 692, in read_binary
2024-10-24 15:32:27 self.file.write(data)
2024-10-24 15:32:27 TypeError: write() argument must be str, not bytes

josephmancuso avatar Oct 24 '24 12:10 josephmancuso

The controller code is added up in the description. Yes , I want to get the posted plain text data on the controller, using request.all() is throwing error.

bikash-kodiary avatar Oct 25 '24 06:10 bikash-kodiary

thank you ill try to replicate this again once more thank you for being patient with me

josephmancuso avatar Oct 25 '24 12:10 josephmancuso