-
Notifications
You must be signed in to change notification settings - Fork 14
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
request POST JSON data is not handled as expected #68
Comments
petschki
changed the title
request POST JSON data is not recognized
request POST JSON data is not handled as expected
Jun 2, 2022
Peter Mathis wrote at 2022-6-2 02:52 -0700:
I sent an XHR call to zope like this:
```
const resp = await fetch("@@backend", {
headers: {
"Content-Type": "application/json",
},
method: "POST",
body: JSON.stringify({
id: "some_id",
_authenticator: "some_authenticator",
}),
});
...
### What I expect to happen:
If I want to read the request in `@@backend` browser view I expect:
```
self.request.form = {
"id": "some_id",
"_authenticator": "some_authenticator",
}
```
### What actually happened:
You have to extract the body data manually via:
```
form_data = json.loads(self.request["BODY"])
```
Zope currently looks at `content-type` only in very rare cases:
only to check for `XML-RPC` requests.
I would not be happy to support `application/json`
in the way you suggest: it works only when the body represents
a structure (then there are names to bind to); it does not work for
e.g. a string or an array (no names to bind to).
I suggest, you use instead the `:json` type converter.
With this, your example could look like:
`await fetch("@@backend?params:json=" + JSON.stringify({...})`
and you would get the JSON decoded value in `request["params"]`.
A similar approach would work for POST requests.
I believe that I already have seen a PR supporting the
`:json` type converter: it might already be available.
Should I be wrong, "zopefoundation/Zope#648"
contains (among other things) such support. It has a good chance
to arrive in Zope 6.
|
-1 for reusing +1 for introducing a new |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What I did:
I sent an XHR call to zope like this:
What I expect to happen:
If I want to read the request in
@@backend
browser view I expect:What actually happened:
You have to extract the body data manually via:
What version of Python and Zope/Addons I am using:
python: 3.9
Zope: master
Plone: 6.0-dev
The text was updated successfully, but these errors were encountered: