Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] Django requests not compatible with interia request payloads? #21

Closed
ChristopherBrownie opened this issue Feb 13, 2023 · 4 comments

Comments

@ChristopherBrownie
Copy link

I've run into another snag. When attempting to post interia js forms the django request only includes the payload in a raw bytestring via request.body.

const form = useForm({
    key: 'value'
});
form.post(usePage().url);

request.POST is empty as that's only populated from traditional form submissions. I'm unable to access key param in the view without processing the request.body bytestring first, which seems totally out of scope for individual projects to handle. DRF has solutions for this, but DRF shouldn't necessarily be required for this adapter to work I wouldn't think? Is there a way for the adapter to handle this?

@ChristopherBrownie ChristopherBrownie changed the title Django requests not compatible with interia request payloads? [Feature Request] Django requests not compatible with interia request payloads? Feb 15, 2023
@ChristopherBrownie
Copy link
Author

Per discord conversation, this is what I have so far (maybe a naive implementation) which could hopefully give people access to inertia request data out of the box.

# settings.py - Setting for parsing application/json requests and placing data in request.JSON. If you are using something like Django Rest Framework, this is done already and available through request.data, so it can be disabled here.
INERTIA_PARSE_JSON_REQUEST = True

# middleware.py
if settings.INERTIA_PARSE_JSON_REQUST and request.body and 'application/json' in request.content_type:
    try:
        request.JSON = json.loads(request.body)
    except json.JSONDecodeError:
        request.JSON = {}

@svengt
Copy link
Contributor

svengt commented Feb 21, 2023

@ChrisxHart You can pass the following argument to form.post to make the request compatible with Django.

form.post(usePage().url, { forceFormData: true });

@BrandonShar
Copy link
Collaborator

I like the idea of baking some json support into Inertia Django because it's such a tightly coupled concept.

@svengt's solution is perfect if you want to maintain Django standards, but for those who don't, I think I'm going to add something very similar to what @ChrisxHart wrote there into core. It'll probably be a very similar syntax, just lazily evaluated.

Thanks both of you, I'll leave this open until I have a PR up.

@BrandonShar
Copy link
Collaborator

closing per #30

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants