How do I modify a POST request body in my middleware before going to database?

I need to use my middleware to be able to modify an incoming POST form-data body enroute to the database.

Before I get a storm of “you don’t do it that way, use a class method in the model, view or serializer” yes I know but I have my reasons.

In my middleware I have one case where the body comes in as follows:

request_post_params= <QueryDict: {'csrfmiddlewaretoken': ['yTAkZ50ecwj1XyzDF2a4RNA8vKKChzDHjeZ85GeM1Qf8pmLhsDAHhVKRXHHqMmuA'], 'matchingKey': ['test'], 'sequence': ['1'], 'param_kwargs_json': ['null'], 'param1': ["poopoo='shite'"], 'param2': ['N/A'], 'param3': ['N/A'], 'param4': ['N/A'], 'param5': ['N/A'], 'param6': ['N/A'], 'param7': ['N/A'], 'param8': ['N/A'], 'param9': ['N/A'], 'param10': ['N/A'], 'description': ['N/A'], '_save': ['Save']}>

The QueryDict is accessible and I can make a local copy, edit the Dict and add my data in ‘param2’ but trying to put it back into the request is the first place I need help.

The other cases include the POST body as a byte string with a multipart form-data with a boundary.

What class handles the multipart form-data with boundaries as it goes into the db? If i could look at that I think I can figure out how to convert it into a dict, make my changes and put it back.

POST data doesn’t go to the database. It is handled by a view. It’s the view’s responsibility to do whatever needs to be done to the data before being saved to the database - including using that POST data to populate models.

This makes it sound a lot like an X-Y Problem. It would be helpful if you identified those reasons - there is likely going to be another way to do this.

From the docs at Request and response objects | Django documentation | Django

The QueryDicts at request.POST and request.GET will be immutable when accessed in a normal request/response cycle.

Your views.

Ok ken, thanks,

When I said,

“Before I get a storm of “you don’t do it that way, use a class method in the model, view or serializer” yes I know but I have my reasons.”

I knew someone, would say something like what you said…yes I know.

Let me try to explain further. When I said I have my reasons, the primary reason is that I do NOT want to modify the code in either the model or view of the application for the simple reason it is NOT my code, it is a licensed product, and if I make changes to their code it has support and maintenance implications, ok? I also do not want to pay them to modify their code if I can find a way.

I also want to apply my changes to more than one entity, so a middleware approach is what I want to use.

Next, I know full well how models, and views work. I also know that when I create my own middleware, I get the request and when a form is posted, the request in the middleware has the request method = POST and the data payload comes in various ways; sometimes it is query_params dict, and sometimes the body is a byte string.

So my question is simple, yes I know it is not normal, I have seen that with the query_params I can make a copy and modify the contents. is it possible to replace the request query_params with my modified copy?

A complication is when the form data with boundary is a byte string in the body. Again, I am referring to the request in the middleware, when I asked what class decodes that byte testing with the multipart form data with boundary. In the middleware it is after the view, is it not? I can only assume there is a class that decodes the byte string prior to going to the database, that is the class I want to look at.

It isn’t the byte string to jason, but the complication of the boundary I am looking to solve.
Lastly, I am considering doing my processing in the middleware in the handle response, and creating an instance of the model class, modifying it and save(), probably easier.

Middleware “brackets” the view. It has the ability to process the request before the view is called and to handle the response after the view is called.

And again - the post data doesn’t go to the database, the post data goes to the view. It’s the responsibility of the view to do whatever is needed with the data - including determining whether or not the data is even going to be sent to the database.

In the general case, POST data is handled by forms, with the addition that file uploads are handled by file upload handlers - again, by the view.

The actual conversion of the http request body into the various parts is handled by the WSGI handler when it converts that body into an HttpRequest - which occurs before any of the middleware is called.

From the description you provide, you may need to create a custom version of WSGIHandler, a new get_wsgi_application, and then change your wsgi.py file to refer to your new function.

Ok thanks, will check that.