GithubHelp home page GithubHelp logo

Comments (8)

dantownsend avatar dantownsend commented on June 11, 2024

Hi @sarvesh4396

My initial thoughts was using middleware to modify the body. I looked around a bit and found this issue:

encode/starlette#495 (comment)

It's definitely not easy to do, but I think it's possible.

We have a couple of ways of modifying the behaviour of PiccoloCRUD. One is hooks, but probably not useful in this situation.

The other is validators. The intended purpose is to add extra validation to endpoints, but you may be able to use these to modify the request.

async def my_validator(piccolo_crud: PiccoloCRUD, request: Request):
    json = await request.json()
    request._json = {**json, 'custom_field': 'hello world'}

It might be worth giving that a go.

from piccolo_api.

sarvesh4396 avatar sarvesh4396 commented on June 11, 2024

hey @dantownsend ,
I tried the above solution but it doesn't work cause the data is extracted before the validators run.

from piccolo_api.

dantownsend avatar dantownsend commented on June 11, 2024

@sarvesh4396 OK, interesting. When you do await request.json() does it just hang?

from piccolo_api.

sarvesh4396 avatar sarvesh4396 commented on June 11, 2024

@dantownsend Yes, It does, query params can be changed through middleware but not the body.
For now, I have passed the modify_body arg for a quick patch it is working fine.

from piccolo_api.

dantownsend avatar dantownsend commented on June 11, 2024

@sarvesh4396 OK, cool. Do you think this is something we could write a custom hook for?

from piccolo_api.

sarvesh4396 avatar sarvesh4396 commented on June 11, 2024

@dantownsend I didn't get it, Do you mean by Hooks which are there in piccolo-api? or something else.
Though I believe we could write a function for it. and we can run it wherever we are doing await request.json() such that we do not have to await again and again which may hamper performance.
accessing the request body in middleware has been an issue I couldn't find anything yet which can resolve it.

from piccolo_api.

dantownsend avatar dantownsend commented on June 11, 2024

@sarvesh4396 Yeah, we have hooks for PiccoloAPI like pre_save. Could add a new one maybe? 🤔

In our CSRFMiddleware we access the request body, but then add it to the ASGI scope. This works OK, but the downstream app needs to know to look in the scope for the data.

if self.allow_form_param and not header_token:
form_data = await request.form()
form_token = form_data.get(self.cookie_name, None)
request.scope.update({"form": form_data})

from piccolo_api.

sarvesh4396 avatar sarvesh4396 commented on June 11, 2024

@dantownsend, It should work ideally, as we can receive the request Object in hooks.

from piccolo_api.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.