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

application/json needs to be handled on the ApiPostController.php #19

Open
asantossmw opened this issue Mar 7, 2019 · 0 comments
Open

Comments

@asantossmw
Copy link

#I was having issues when submitted data to the backend, the controller was unable to get the message parameter. In order to process requests that have Content-Type: application/json header defined I added the following code on the ApiPostController.php file:

    /**
     * @Rest\Post("/api/post/create", name="createPost")
     * @param Request $request
     * @return JsonResponse
     * @IsGranted("ROLE_FOO")
     */
    public function createAction(Request $request): JsonResponse
    {
        // Added this in order to handle requests with Content-Type: application/json
        if (0 === strpos($request->headers->get('Content-Type'), 'application/json')) {
            $data = json_decode($request->getContent(), true);
            $request->request->replace(is_array($data) ? $data : array());
        }
        $message = $request->request->get('message');
        $postEntity = $this->postService->createPost($message);
        $data = $this->serializer->serialize($postEntity, 'json');
        return new JsonResponse($data, 200, [], true);
    }

I don't know if this is the best way to do this, I'm new to Symfony.

BTW, thanks for creating this tutorial / guide was very helpful :)

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

1 participant