Comments (17)
@spacedog4 please create a new issue instead, but the explanation you gave so far is a bit vague.
from mezzio.
I've tried to use $request->getBody()->getContents() and it shows the body params in string format. I don't know why the getParsedBody() returns an empty array.
You may try this: https://gist.github.com/gabbydgab/0193c4b6a8e02f658e9b5f7b7650235d
Originally posted by @gabbydgab at zendframework/zend-expressive#371 (comment)
from mezzio.
It seems it only works with application/json type.
See zendframework/zend-expressive-helpers#19
Originally posted by @gabbydgab at zendframework/zend-expressive#371 (comment)
from mezzio.
From the docs (the first link you gave):
Body Parsing Middleware
By default, this middleware will detect the following content types:
- application/x-www-form-urlencoded (standard web-based forms, without file uploads)
- application/json, application/*+json (JSON payloads)
Originally posted by @geerteltink at zendframework/zend-expressive#371 (comment)
from mezzio.
Even using the body parsing middleware, on POST request the getParsedBody() is not empty, but when PUT request is sent, it's always empty for some reason.
Originally posted by @loter at zendframework/zend-expressive#371 (comment)
from mezzio.
@gabbydgab Which version of zend-expressive-helpers
are you using? There was a fix pertaining to JSON requests and parsed body in 2.0.1
(which was released after you opened this issue) that may be related. You might want to check on that as well, @loter.
If that's not the issue, let me know and we can investigate further.
Originally posted by @michaelmoussa at zendframework/zend-expressive#371 (comment)
from mezzio.
@michaelmoussa Ah, that would make sense since I can't replicate it. I should read the changelogs more often :)
Originally posted by @geerteltink at zendframework/zend-expressive#371 (comment)
from mezzio.
@gabbydgab The getParsedBody()
works only with GET/POST for application/x-www-form-urlencoded
. XHTML 1.x forms only support GET and POST.
That said, you can also use PUT with x-www-form
but you need to parse data using php://input
source, because there isn't a $_PUT
global. Here you can read an article on how to implement it.
Originally posted by @ezimuel at zendframework/zend-expressive#371 (comment)
from mezzio.
Actually... this should work. For people this affects, we need the following information:
- What is the value of the
Content-Type
header? - What HTTP request method were you using?
- What was the body content provided by the client?
- What does
(string) $request->getBody()
return?
Originally posted by @weierophinney at zendframework/zend-expressive#371 (comment)
from mezzio.
@ezimuel has indicated that he encountered the problem with Content-Type: application/json; charset=utf-8
. This makes sense that it would fail at this time, as our matching is for the pattern #[/+]json$#
currently. This should likely be updated to split the content-type at a ;
boundary:
$parts = explode(';', $contentType, 2);
$contentType = $parts[0];
// perform match
@ezimuel Would you like to prepare a PR? If not, I'll do it next week.
Originally posted by @weierophinney at zendframework/zend-expressive#371 (comment)
from mezzio.
@weierophinney isn't the strategy as you mentioned in code currently https://github.com/zendframework/zend-expressive-helpers/blob/4aea21e4102661cdafe9ea6d5b45944a68bb8919/src/BodyParams/JsonStrategy.php#L22 .
Originally posted by @harikt at zendframework/zend-expressive#371 (comment)
from mezzio.
Unfortunately, when I send JSON using POST/PUT request and then do $request->getParsedBody()
, I get an empty array()
, too.
My scenario in POSTMAN:
- I use
{"Content-Type":"application/json"}
- It happens on POST / PUT / PATCH request
- I send a simple JSON structure like
{"email":"[email protected]","password":"1234"}
- When I do
(string) $request->getBody()
gets the actual json in string format, and also I implemented an function in my AbstractController to handle the raw body like this:
(array)json_decode($request->getBody()->getContents());
which I consider a rustly way to parse JSON having thegetParsedBody()
. Does anyone has an update on this?
Originally posted by @lvidal1 at zendframework/zend-expressive#371 (comment)
from mezzio.
@harikt Ooof, you're right. I'll still work up a PR with at least the failing test, though, as it's not working. Something's amiss.
@lvidal1 — One issue with getContents()
is that it grabs contents from the current pointer. So, if the pointer is already at the end, you'll get an empty string. As such, you should likely either do a $body->rewind()
first, or just cast getBody()
to a string.
Originally posted by @weierophinney at zendframework/zend-expressive#371 (comment)
from mezzio.
I've created a unit test intended to reproduce the issue... but it doesn't. It passes. If this issue affects you, please look at zendframework/zend-expressive-helpers#43, and let me know if any changes need to be made to the tests proposed.
Originally posted by @weierophinney at zendframework/zend-expressive#371 (comment)
from mezzio.
Well, I'm really happy to have discovered this thread !
I was really struggling with $request->getParsedBody()
, it was always empty when (string)$request->getBody()
was not empty (exactly as described by @lvidal1 : zendframework/zend-expressive#371 (comment)).
Following zendframework/zend-expressive-helpers#43, I added Zend\Expressive\Helper\BodyParams\BodyParamsMiddleware::class
in my pipeline and now it works just fine ! 🎉
Originally posted by @jbelien at zendframework/zend-expressive#371 (comment)
from mezzio.
Thank you for this thread! It took me forever to figure out that I needed to add BodyParamsMiddleware
to my pipeline. I spent about 3 days racking my brain over this until tracing through and figuring out that $request->getParsedBody()
was the culprit, returning an empty array.
Originally posted by @ramsey at zendframework/zend-expressive#371 (comment)
from mezzio.
I suggest closing this issue as the final comment covers the correct resolution.
from mezzio.
Related Issues (20)
- Create `Mezzio\ApplicationPipeline` implementation for better static code analysis HOT 1
- Missing good examples of configuration template paths for laminas-view in mezzio HOT 3
- Replace `RequestHandlerRunner` factory with `RequestHandlerRunnerInterface` factory HOT 3
- Page with the description of the installation is missing
- Dependency Dashboard
- [RFC]: Provide an ApplicationProgrammaticConfigDelegator that pairs with ApplicationConfigInjectionDelegator HOT 4
- Script MezzioInstaller\OptionalPackages::install handling the pre-install-cmd event terminated with an exception HOT 3
- Upgrading to PHP 8.2 breaks Dependency Injection HOT 9
- [RFC]: Remove Pimple from Mezzio documentation and from Mezzio skeleton HOT 3
- bin/mezzio-tooling is incompatible with mezzio/mezzio-tooling v2 HOT 7
- [RFC]: Deprecate `ApplicationConfigInjectionDelegator` and `Application#post`, etc.
- Provide `RouteCollectorInterface` and `MiddlewarePipeInterface` delegators to supersede `ApplicationConfigInjectionDelegator` HOT 1
- jsoumelidis/zend-sf-di-config[0.4.0, ..., 0.4.1] require php ^7.1 -> your php version 8.0.99 HOT 1
- PhpDebugBar documentation is outdated. HOT 1
- [RFC]: Remove Aura.Di from Mezzio documentation and from Mezzio skeleton HOT 1
- [RFC]: Add Symfony DependencyInjection as container HOT 2
- [RFC]: Add chubbyphp-container as container
- Move router integration tests to router packages
- PHP 8.1 Support HOT 12
- Support for laminas-httphandlerrunner 2.x versions HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mezzio.