Guzzle 6,提出要求和描述 json

Guzzle 6, put request and description json

我使用带有 json 文件的 guzzle 6 来描述我的调用方法。

下面是一个带有放置请求的示例:

在 json 描述符文件中:

"putObjects" : {
    "httpMethod": "PUT",
    "uri": "objects",
    "summary": "Send objects to the api",
    "parameters": {
        "objects" : {
            "type" : "string",
            "location" : "body"
        }
    }
}

在 symfony 控制器中:

$clientResponse = $client->execute(
    $client->getCommand("putObjects", array(
        'objects'       => $request->getContent()
    ))
);

之前,使用 guzzle 3 发送 put 请求时,发送的数据格式如下(有效 json):

{对象:[{....}]}

但是现在,对于 guzzle 6,数据格式为:

对象={对象:[{....}]}

当然还有我的 api 向我发送错误消息 'Invalid json message received'。

有人对这个问题有想法吗?

我找到了解决方案。

在 symfony 控制器中:

$content = json_decode($request->getContent(), true);

$clientResponse = $client->execute(
    $client->getCommand("putObjects", array(
        'objects'       => $content['objects']
    ))
);

在 json 描述符文件中:

"putObjects" : {
    "httpMethod": "PUT",
    "uri": "objects",
    "summary": "Send objects moderated",
    "parameters": {
        "objects" : {
            "type" : "array",
            "location" : "json"
        },
    }
}