首页 > 解决方案 > Guzzle 从响应对象中检索 form_params

问题描述

我正在尝试为 GraphQL 调用编写单元测试,但由于“form_params”字段与 GuzzleHttp\Psr7\Stream 对象内容不匹配而失败。我正在查看使用 REST api 调用的现有单元测试,并尝试使用 Guzzle 打印出其“form_params”的内容。在文档中找不到任何内容

$url = \GuzzleHttp\uri_template($url[0], $url[1]);

        $options = [
            'form_params' => [
                'enrollment[user_id]' => 'sis_user_id:' . $userId,
                'enrollment[type]' => $enrollmentType,
                'enrollment[enrollment_state]' => $enrollmentState,
                'enrollment[role_id]' => $role_id
            ]
        ];

//the response 
$response = $this->client->post($url, $options);
var_dump($response->getBody()->getContents()); // trying to get form_params

转储行给了我这个类似于 form_params 但不完全

"{"sis_section_id":"Section-S15-99190","type":"TeacherEnrollment","role":null,"user":{"id":1,"sis_user_id":234,"login_id":"thackerj","name":"Jeremy Thacker"},"grades":{"unposted_current_grade":null,"unposted_final_grade":null}}"

标签: phpapiguzzle

解决方案


$client = new Client([
    'headers' => [
        'Accept' => 'application/json',
        'Content-Type' => 'application/x-www-form-urlencoded'
    ]
]);

推荐阅读