接收到无效的json消息

时间:2018-11-29 09:21:46

标签: symfony doctrine-orm doctrine swagger

我将Symphony 4与Doctrine结合使用,现在实现/添加Swagger,因此我将拥有适当的文档来测试我的api。

这是我的邮递员,输入了正确的表格数据,并给出了不错的答复。 enter image description here

这是我对Swagger的“有问题”表示法

 * @Route(
 *     "/request",
 *     name="create",
 *     methods={"POST"}
 * )
 *
 * @SWG\Parameter(
 *     name="domain",
 *     in="formData",
 *     required=true,
 *     type="array",
 *          @SWG\Items(
 *             type="string"
 *          )
 * ),
 *
 * @SWG\Parameter(
 *     name="name",
 *     in="formData",
 *     required=true,
 *     type="string",
 * ),
 *
 * @SWG\Response(
 *     response=200,
 *     description="OK",
 *     @SWG\Schema(
 *          type="array",
 *          @Model(type=App\Entity\Request::class)
 *     )
 * ),
 *
 * @SWG\Response(
 *     response=201,
 *     description="Request created",
 *     @SWG\Schema(
 *          type="array",
 *          @Model(type=App\Entity\Request::class)
 *     )
 * ),

所以现在我通过myurl.com/api/doc打开Swagger并尝试执行相同的操作... enter image description here

所以我的招摇会在卷曲请求之后执行

curl -X POST "http://certify.test/request" -H "accept: application/json" -H "Content-Type: application/json" -d "domain=antrax.com,www.antrax.com&name=antrax.com%2Cwww.antrax.com"

答案是

{
  "status": "error",
  "code": 0,
  "message": "Invalid json message received"
}

可以,请帮助我正确注释我的招摇参数,以便我的API文档能够提出正确的请求。如果您需要任何其他信息,请告诉我,我会提供。谢谢!

1 个答案:

答案 0 :(得分:4)

我认为Swagger发送的 Content-type 不正确。

使用邮递员,您将发送一个等于application/x-www-form-urlencoded Content-type 标头,但是使用Swagger,它等于application/json

尝试在您的Swagger批注中添加consumes键,

 * @SWG\Post(
 *     path="/request",
 *     consumes={"application/x-www-form-urlencoded"},
 * )

此外,您还应该将domain参数更改为domain[]

@SWG\Parameter(
*     name="domain[]",
*     in="formData",
*     description="Array of domains for TSL certificate",
*     required=true,
*     type="array",
*          @SWG\Items(
*             type="string"
*          )
* )