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

Slim/Crud integration sends request body as an array #567

Closed
kdoe13 opened this issue Aug 16, 2019 · 8 comments
Closed

Slim/Crud integration sends request body as an array #567

kdoe13 opened this issue Aug 16, 2019 · 8 comments
Assignees
Labels

Comments

@kdoe13
Copy link

kdoe13 commented Aug 16, 2019

My current project begins with a request going through Slim and middleware passing the request into the CRUD. The "getParsedBody()" of the Request object is an array, at this point, not an object...so assuming the request comes in as

POST /records/posts
Content-Type: application/json

{"user_id":1,"category_id":1,"content":"test"}

by the time it gets to the CRUD and specifically to https://github.com/mevdschee/php-crud-api/blob/master/src/Tqdev/PhpCrudApi/Controller/RecordController.php#L75-L80, the record is an array..so on line 80, this will be true and it won't send all the data on to $this->service->create() as expected.

I have some work-arounds for this, but just not sure if this is an error in my setup or just a feature, that isn't working well for me.

Thank you, I really like how versatile the CRUD is.

@mevdschee mevdschee self-assigned this Aug 16, 2019
@mevdschee mevdschee added the bug label Aug 16, 2019
@mevdschee
Copy link
Owner

Hi, Thank you for your bug report. I really appreciate the effort and the quality of your issues.

The "getParsedBody()" of the Request object is an array, at this point, not an object

Can you disable your middlewares and see whether or not this is specific for your implementation?

Kind regards, Maurits

@kdoe13
Copy link
Author

kdoe13 commented Aug 16, 2019

Just to double check on what you are asking...you would like for me to pass the outside request directly to the CRUD without going through my Slim application first?

Also, so that you can see what I'm currently doing as a bypass that works, I'm attaching the relevant code.

use \Psr\Http\Message\RequestInterface;
use \Psr\Http\Message\ResponseInterface;
use Tqdev\PhpCrudApi\Config;
use Tqdev\PhpCrudApi\Api;

$requestBody = $request->getParsedBody();
$request = $request->withParsedBody((object)$requestBody);

$response = $api->handle($request);

@mevdschee
Copy link
Owner

Hi Karl. This is what I wanted to know, thank you. I will try to reproduce and come up with a solution (if I can). Kind regards, Maurits

mevdschee added a commit that referenced this issue Aug 16, 2019
@mevdschee
Copy link
Owner

mevdschee commented Aug 16, 2019

I have found the reason, SlimPHP does not allow you to send objects, see:

https://github.com/slimphp/Slim/blob/3.x/Slim/Http/Request.php#L200

IMHO that line should have read (leaving out the second parameter):

$result = json_decode($input);

This is a mistake on their side (in Slim 3) and it is no longer present in Slim 4.

Nevertheless I was able to implement a workaround (see commit). Can you test it?

mevdschee added a commit that referenced this issue Aug 16, 2019
@mevdschee
Copy link
Owner

mevdschee commented Aug 16, 2019

I have released v2.4.1 for you. It should have the (improved) workaround.

@mevdschee
Copy link
Owner

mevdschee commented Aug 16, 2019

You may also read about SlimPHP 4 support on my blog:

https://tqdev.com/2019-automatic-api-slimphp-4

Please reopen this issue if you are still experiencing problems.

@kdoe13
Copy link
Author

kdoe13 commented Aug 16, 2019

This is working for me as expected (double checked). I will upgrade to Slim 4, though, as well, wasn't paying enough attention to that update. Thanks for being so diligent in your tracking this down when there was no reason for you to know I'd be behind on SLIM versioning and I wouldn't expect you to keep your code up to date with a previous version.

Thanks for all of this, learned a lot today in my interactions back and forth with you and really really appreciate your time!

@mevdschee
Copy link
Owner

Thank you for your kind words, appreciated!

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

No branches or pull requests

2 participants