httpClient Symfony中的Json选项引发错误



我在symfony上使用httpClient,并调用API我想使用json选项而不是使用body,但它不起作用,当我使用body并以json格式键入时,一切都起作用,但我觉得它不干净,所以我不想使用只适用于json=>等简单变量的json选项;['var1'=>'value1,'var2'=>'value2'…]

但一旦我使用数组,它就不起作用了,我得到了这个错误:

The type of the key "firstname" must be "int", "string" given.

请参阅下方的我的代码

$procedure = $this->httpClient->request(
'POST',
"https://fakeurl.com",
[
'headers' =>
[
'Accept' => 'application/json',
'Content-Type' => 'application/json',
],
'auth_bearer' => "key",
'json' => [
"name" => "name",
"description" => "description",
"start"  => true,
"members" => [
"firstname" => $user->getFirstName(),
"lastname" => $user->getLastName(),
"email" => $user->getEmail(),
"phone" =>"+3312345678",
"fileObjects" => [
"file" =>$file['id']
]
]
]
]
);

我刚刚找到了解决方案;成员";期望一个数组,所以正确的方法是:

$procedure = $this->httpClient->request(
'POST',
"https://fakeurl.com",
[
'headers' =>
[
'Accept' => 'application/json',
'Content-Type' => 'application/json',
],
'auth_bearer' => "key",
'json' => [
"name" => "name",
"description" => "description",
"start"  => true,
"members" => [[
"firstname" => $user->getFirstName(),
"lastname" => $user->getLastName(),
"email" => $user->getEmail(),
"phone" =>"+3312345678",
"fileObjects" => [[
"file" =>$file['id']
]]
]]
]
]
);

相关内容

最新更新