Nelmioapidocbundle: Error Code 400: Invalid json message received

Created on 18 Jan 2018  路  16Comments  路  Source: nelmio/NelmioApiDocBundle

Actually everything worked fine with Symfony 3.3. Today i've upgraded to 3.4 and as soon as i Post Parameters with the Sandbox i receive a Error 400 - Invalid json message received. Same Request with GuzzleHTTP from phpunit works. I debugged it, and to me the Curl Request looks wrong:

curl -X POST "/rest/function/create" -H "accept: application/json" -H "Content-Type: application/json" -d "a=b&c=d&e=f"

@json_decode can't decode the String, which could be true, the Body does not contain json? To me this looks like normal URL Parameters?

Most helpful comment

Ok I fixed it by adding :

consumes={"multipart/form-data"},

It only impact swagger, it still still accept application/json payload.

Edit : It seems the bug comme from swagger-ui : https://github.com/swagger-api/swagger-ui/issues/3583

All 16 comments

Btw. NelmioApiDocBundle 3.0 (same error with dev-master) and FosRestBundle.

Not sure what's the issue here, could you provide us a screenshot of your route doc so that we understand it better ?

The Routes are OK, Symfony receives the Request from the Sandbox. Only the "Invalid JSON Message" shows up. I didn't understand which Screenshot you want, a Screenshot of the opened Sandbox?

Same error here

dump

According to https://stackoverflow.com/a/35422195/6140843, you must use a body parameter to have a json request.

Same here, i don't want a json request, after try the error is in the HEADER not the response:

This not work :
curl -X POST "url" -H "Content-Type: application/json" -d "_username=user&_password=pass"

this work
curl -X POST "url" -d "_username=user&_password=pass"

How could i remove the Content-Type ?

Same problem here.

The api doc send Content-Type:application/json , but with a urlencoded payload.

Same question, have you tried using body instead of formData?

I use swagger annotation.

   /**
     * @Operation(
     *     summary="Add user",
     *     tags={"Users"},
     *     @SWG\Parameter(
     *         name="email",
     *         in="formData",
     *         description="The email of the user",
     *         required=false,
     *         type="string"
     *     ),
     *     @SWG\Parameter(
     *         name="firstName",
     *         in="formData",
     *         description="First name of the user",
     *         required=true,
     *         type="string"
     *     ),
     *     @SWG\Parameter(
     *         name="lastName",
     *         in="formData",
     *         description="Last name of the user",
     *         required=true,
     *         type="string"
     *     ),
     *     @SWG\Parameter(
     *         name="birthday",
     *         in="formData",
     *         description="Birthday (YYYY-MM-DD)",
     *         required=true,
     *         type="string",
     *         default="1990-01-01"
     *     ),
     *     @SWG\Response(
     *         response="200",
     *         description="On success, return created user."
     *     ),
     * )
     *
     * @Rest\Post("/api/users", name="api/users/post")
     *
     * @param Request     $request
     * @param UserService $userService
     * @param Extractor   $extractor
     *
     * @return \Symfony\Component\HttpFoundation\Response
     *
     * @throws \Exception
     */

It works well before.

If I replace formData by body, I have this error :

User Notice: Field "schema" is required when @SWG\Parameter(name="email",in="body") is in "body" .

Ok I fixed it by adding :

consumes={"multipart/form-data"},

It only impact swagger, it still still accept application/json payload.

Edit : It seems the bug comme from swagger-ui : https://github.com/swagger-api/swagger-ui/issues/3583

Got same issue with sf4 and nelmio3.2
fixed by replacing files in public/bundles/nelmioapidoc/swagger-ui with fresh ones from swagger-ui v3.13.1

PS Not sure which version of swagger-ui was used in nelmio3.2, but I would be glad if someone release a hotfix with updated swagger-ui

any solution?

+1

I have the same Issue Here.
Using Symfony 4.1, nelmio/api-doc-bundle v3.2.1 and zircote/swagger-php 2.0.13

curl -X POST "http://localhost:5200/api/battle" -H "accept: application/json" -H "Content-Type: application/json" -d "userToken=sometoken&json=%7B%22some%22%3A%22json%22%7D"

If I perform the same curl request without Content-type, the request is working.

Fix it please.

+1

Was this page helpful?
0 / 5 - 0 ratings